拦截Jquery AJAX请求以插入其他变量? [英] Intercepting a Jquery AJAX request to insert additional variables?
问题描述
有没有办法拦截通过jquery发出的ajax请求,在其中插入其他变量?
Is there any way to intercept an ajax request being made via jquery, to insert additional variables into it?
我知道。ajaxStart()允许您注册一个回调,该回调会在ajax请求开始时触发事件,但我正在寻找的是一种方法取消ajax请求,如果它满足某些条件(例如url),在其内容中插入更多变量,然后提交它。
I know that .ajaxStart() lets you register a callback which triggers an event whenever an ajax request begins, but what I'm looking for is a way to cancel that ajax request if it meets certain criteria (e.g url), insert some more variables into its content, and then submit it.
这是针对第3个插件的插件派对软件,其代码无法直接更改。
This is for a plugin for a 3rd party software whose own code can't be changed directly.
编辑:似乎。ajaxSetup()允许您设置一些与ajaxRequests相关的全局变量。如果我注册了beforeSend函数,该函数是否能够取消请求以满足某些条件的不同?
Seems like .ajaxSetup() lets you set some global variables related to ajaxRequests. If I registered a beforeSend function, would that function be able to cancel the request to make a different one on meeting certain criteria?
推荐答案
想出来,这是我使用的代码:
Figured it out, this was the code I used:
jQuery(document).ready(function()
{
var func = function(e, data)
{
//data.data is a string with &seperated values, e.g a=b&c=d&.. .
//Append additional variables to it and they'll be submitted with the request:
data.data += "&id=3&d=f&z=y";
return true;
};
jQuery.ajaxSetup( {beforeSend: func} );
jQuery.post('example.php', {a : 'b'}, 'json');
} );
要取消请求,请从 func
似乎工作。
To cancel the request, returning false from func
seemed to work.
这篇关于拦截Jquery AJAX请求以插入其他变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!