5

有什么方法可以拦截通过 jquery 发出的 ajax 请求,将其他变量插入其中?

我知道变量写入其内容,然后提交。

这是用于无法直接更改自己的代码的 3rd 方软件的插件。

编辑:似乎.ajaxSetup()允许您设置一些与 ajaxRequests 相关的全局变量。如果我注册了一个 beforeSend 功能,该功能是否能够取消满足某些条件的请求以创建不同的请求?

4

2 回答 2

9

想通了,这是我使用的代码:

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');    
} );

要取消请求,返回 false fromfunc似乎可行。

于 2012-12-28T01:09:14.807 回答
1

两种方法可以拦截 ajax 调用并向其中插入附加数据:javascript 方法和 jQuery 方法:

javacript 方法

(function(send) {
    XMLHttpRequest.prototype.send = function(body) {
        var info="send data\r\n"+body;
        alert(info);
        send.call(this, body);
    };

jQuery 方法

$.ajaxSetup({
    beforeSend: function (xhr,settings) {
        alert(settings.data);
        alert(settings.url);
    }
});
于 2018-03-12T05:33:28.873 回答