diff options
Diffstat (limited to 'lib/util.js')
-rw-r--r-- | lib/util.js | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/util.js b/lib/util.js index 02fd476..b165567 100644 --- a/lib/util.js +++ b/lib/util.js @@ -262,3 +262,38 @@ exports.loadJSON = function loadJSON(URL, cb_function, what) { cb_function.call(what, data); }, what); }; + +/** + * run HTTP POST request + * + * @param URL String with URL; required + * @param data Object/String with data ; required + * @param cb_function Function called when the request succeeds, with + * the only parameter being request object ; required + * @param what Object which will represent this for the cb_function ; optional + * @param mimeData String with MIME type of data + * @param mimeGet String with MIME type expected on return + */ +exports.httpPOST = function httpPOST(URL, data, cb_function, what, mimeData, mimeGet) { + what = what === undefined ? this : what; + mimeData = mimeData === undefined ? "application/x-www-form-urlencoded" : mimeData; + mimeGet = mimeGet === undefined ? "text/plain" : mimeGet; + + var req = new xhrMod.XMLHttpRequest(); + console.log("req = " + req.toSource()); + req.open("POST", URL, true); + + req.overrideMimeType(mimeGet); + req.setRequestHeader("Content-type", mimeData); + req.onreadystatechange = function(aEvt) { + if (req.readyState === 4) { + if (req.status === 200) { + console.log("POST success!"); + cb_function.call(what, req); + } else { + console.error("POST failed!"); + } + } + }; + req.send(data); +}; |