标签:
1、xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
这个设置只有post方法才用的
2、x-requested-with XMLHttpRequest是Ajax 异步请求方式
使用
request.getHeader("x-requested-with");
为 null,则为传统同步请求;
为 XMLHttpRequest,则为 Ajax 异步请求。
function ajax (url, parms) { parms = parms || {}; var req = new XMLHttpRequest(), post = parms.post || null, callback = parms.callback || null, timeout = parms.timeout || null; req.onreadystatechange = function () { if ( req.readyState != 4 ) return; // Error if ( req.status != 200 && req.status != 304 ) { if ( callback ) callback(false); return; } if ( callback ) callback(req.responseText); }; if ( post ) { req.open(‘POST‘, url, true); req.setRequestHeader(‘Content-type‘, ‘application/x-www-form-urlencoded‘); } else { req.open(‘GET‘, url, true); } req.setRequestHeader(‘X-Requested-With‘, ‘XMLHttpRequest‘); req.send(post); if ( timeout ) { setTimeout(function () { req.onreadystatechange = function () {}; req.abort(); if ( callback ) callback(false); }, timeout); } }
调用方式:
ajax(‘dataset.php?start=‘ + +start + ‘&count=‘ + +count, { callback: function (data) { data = JSON.parse(data); myScroll.updateCache(start, data); } });
标签:
原文地址:http://www.cnblogs.com/alplcx/p/4870220.html