如何使用 jQuery 执行以下操作:
- 一些库通过以下方式发送ajax请求 $.ajax
- 我需要捕获所有这些 请求,并在某些情况下中止 他们,而不是冒充另一个 数据。
我发现,在jQuery 1.5
中引入了新方法,例如ajaxPrefilter
和ajaxTransport
。我还尝试了 ajaxSetup
和 beforeSend
,
但我无法实现其中的 2 点...
最佳答案
除非您非常确定自己在做什么,否则不要使用此功能
我不确定 ajax 拦截器库。但我可以告诉你令人讨厌的黑客
复制原始 jquery ajax 实例
var oldAjaxInstance; //some global variable oldAjaxInstance = $.ajax; //in document load
并将你的 intercepert 方法分配给 $.ajax 指针
$.ajax = myAjaxwrapper;
myAjaxwrapper 看起来像这样
function myAjaxwrapper(a) {
//your logic to change the request data's
if (you are ok to allow the ajax call) {
//re Assgin the actual instance of jquery ajax
$.ajax =oldAjaxInstance;
//and call the method
$.ajax(a);
}
//Otherwise it wont be called
}
ajax 调用成功后,将 ajax 包装器重新分配给 jquery ajax
oldAjaxInstance = $.ajax; $.ajax = myAjaxwrapper;
关于javascript - jQuery ajax 请求代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4885449/