Skip to content

Instantly share code, notes, and snippets.

@biboletin
Last active February 17, 2018 13:41
Show Gist options
  • Save biboletin/0d5821de4c9a03b2a758ac245325dd0f to your computer and use it in GitHub Desktop.
Save biboletin/0d5821de4c9a03b2a758ac245325dd0f to your computer and use it in GitHub Desktop.
var Request = {
method: "POST",
send: function(url, data, callback){
var dataToSend = this.QueryString(data);
var ajax = new XMLHttpRequest();
ajax.open(this.method, url, true);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.onreadystatechange = function(){
if(ajax.readyState == 4 && ajax.status == 200){
return callback(ajax.responseText);
}
};
ajax.send(dataToSend);
},
QueryString: function(params){
var str = "";
if(typeof params === "object" && params.constructor == Object){ // това да се оправи
for(var value in params){
str += value + "=" + encodeURIComponent(params[value]) + "&";
}
}
if(str.length > 0){
str = str.substring(0, str.length - 1);
}
return str;
}
};
/*
How to use:
Request.send("ajax.php", {
"name1": "value_1",
"name2": "value_2",
"name3": "value_3"
},
function(result){
console.log(result);
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment