Created
April 12, 2016 03:10
-
-
Save buaawp/1c9e503c460a3f6dc1a4eac77fffdc06 to your computer and use it in GitHub Desktop.
修改XMLHttpRequest发送方法,更改Ajax目标url。(不支持跨域)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 修改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