Skip to content

Instantly share code, notes, and snippets.

@buaawp
Created April 12, 2016 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buaawp/1c9e503c460a3f6dc1a4eac77fffdc06 to your computer and use it in GitHub Desktop.
Save buaawp/1c9e503c460a3f6dc1a4eac77fffdc06 to your computer and use it in GitHub Desktop.
修改XMLHttpRequest发送方法,更改Ajax目标url。(不支持跨域)
/**
* 修改XMLHttpRequest发送方法,更改Ajax目标url
*/
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;
}
}
// // 创建script标签,用于发起jsonp调用
// var script = document.createElement('script');
// script.setAttribute('src', options.url);
// // 把script标签加入head,此时调用开始
// document.getElementsByTagName('head')[0].appendChild(script);
this._open(options.method, url, options.async);
};
/**
* 更改Ajax目标url
*/
window.beforeXMLHttpRequestSend = function(xhr, options) {
//重置参数
options.url = 'http://127.0.0.1:8000/myjsonp/getjsonforajax?url='+options.url;
//options.method = 'PUT';
//options.async = false;
//禁止发送请求
//return false;
//发送请求
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment