var serialize = function(obj, prefix) {
var str = [], p;
for(p in obj) {
if (obj.hasOwnProperty(p)) {
var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
str.push((v !== null && typeof v === "object") ?
serialize(v, k) :
encodeURIComponent(k) + "=" + encodeURIComponent(v));
}
}
return str.join("&");
};
function postAjax(url, data, success) {
var params = typeof data == 'string' ? data : serialize(data);
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xhr.open('POST', url);
xhr.onreadystatechange = function() {
if (xhr.readyState>3 && xhr.status==200) {
try {
var obj = JSON.parse(xhr.responseText);
success(obj);
} catch(ex) {
console.log('Error: '+ex.message+'\nTrace: '+xhr.responseText);
}
}
};
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(params);
return xhr;
}
March 9, 2018
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment