public
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
function parseQuery(json) {
var q = "";
var first = true;
for (var key in json) {
if(json[key]=="" || json[key]==null) continue;
if (first) {
first = false;
q += (key + "=" + json[key]);
} else {
q += ("&" + key + "=" + json[key]);
}
}
return q;
}
 
function httpRequest(method, url, data) {
var xhr = new XMLHttpRequest();
 
//ready
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200 || xhr.status == 201) {
var responseText = xhr.responseText;
var responseObj = JSON.parse(responseText);
//refresh model
clear();
insertList(responseObj.data);
requestFinished(responseObj.success, method, responseObj.data);
} else {
requestFinished(false, method, "request failed (" + xhr.statusText + ")");
}
}
}
 
//open
console.log(url);
xhr.open(method, url);
 
//send
if (data) {
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(parseQuery(data))
} else {
xhr.send();
}
}
 
httpRequest("PUT", "https://xxx.xxx.com", {"done":1});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.