123456789101112131415161718192021222324 |
- function postJson(url, data, callback) {
- var r = new XMLHttpRequest();
- r.open("POST", url, true);
- r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- r.onreadystatechange = function () {
- if (r.readyState != 4 || r.status != 200) return;
- var response = JSON.parse(r.responseText);
- callback(response);
- };
- r.send(urlparams(data));
- }
- function urlparams(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("&");
- }
|