jQuery已经实现了各类 ajax 事件,如 beforeSend 等,但原生的 XMLHttpRequest 并无这种事件。在没有用 jQuery的状况下,若是想修改 ajax 请求,得作一些特殊的处理。如下是我处理的方法:ajax
XMLHttpRequest.prototype._open = XMLHttpRequest.prototype.open; XMLHttpRequest.prototype.open = function(method, url, async) { // 用对象便于修改参数 var options = { method: method, url: url, async: async }; if('function' === typeof window.beforeXMLHttpRequestSend) { if(!window.beforeXMLHttpRequestSend(this, options)) { return; } } this._open(options.method, options.url, options.async); }; window.beforeXMLHttpRequestSend = function(xhr, options) { //重置参数 options.url = 'reset url'; options.method = 'PUT'; options.async = false; //禁止发送请求 //return false; //发送请求 return true; };