Skip to content

Instantly share code, notes, and snippets.

@mosluce
Created February 20, 2013 08:05
Show Gist options
  • Save mosluce/4993820 to your computer and use it in GitHub Desktop.
Save mosluce/4993820 to your computer and use it in GitHub Desktop.
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});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment