这段代码自定义了一个简单的微型ajax框架,如果只需要只用简单的ajax post,get等方法,完全没有必要是使用jQuery那种巨型框架,使用下面这个小的ajax框架即可
var ajax = { xhr: new XMLHttpRequest (), request: function (method, url, vars) { vars = JSON.stringify(vars); vars = vars.replace(/\{/g, '').replace(/\}/g, '').replace(/\"/g, '').replace(/:/g, '=').replace(/,/g, '&'); ajax.xhr.open(method, url, true); ajax.xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); ajax.xhr.send(vars); }, response: function (res) { ajax.xhr.onreadystatechange = function () { return res(ajax.xhr.responseText); }; }, get: function(url, vars, res) { ajax.request('GET', url, vars); ajax.response(res); }, post: function(url, vars, res) { ajax.request('POST', url, vars); ajax.response(res); } }; /* [object] ajax [method] get|post @param [string] url @param [object/json] vars @param [function] res (callback) */ ajax.post( 'login.php', { 'username':'demo', 'password':'demo' }, function (res) { console.log(res); } ); This looks like a JavaScript file. Click this bar to format it.No 4