function json2url(json) { var arr = []; for (var name in json) { arr.push(name+'='+encodeURIComponent(json[name])); } return arr.join('&'); } function ajax(json) { json.data = json.data || {}; json.data.t = Math.random(); json.type = json.type || 'GET'; if (window.XMLHttpRequest) { var oAjax = new XMLHttpRequest(); } else { var oAjax = new ActiveXObject('Microsoft.XMLHTTP'); } switch (json.type.toLowerCase()) { case 'get': oAjax.open('GET', json.url + '?' + json2url(json.data), true); oAjax.send(); break; case 'post': oAjax.open('POST', json.url, true); oAjax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); oAjax.send(json2url(json.data)); } oAjax.onreadystatechange = function () { if (oAjax.readyState == 4) { if (oAjax.status >= 200 && oAjax.status < 300 || oAjax.status == 304) { json.success && json.success(oAjax.responseText); } else { json.erro && json.erro(oAjax.status); } } }; }