如何将变量绑定到jquery ajax请求?

时间:2022-08-23 11:07:30

This is self-explanatory:

这是不言自明的:

while (...) {
    var string='something that changes for each ajax request.';
    $.ajax({'type': 'GET','dataType': 'json', 'url': 'get_data.php'}).done(processData);
}
function processData(data) {
    // get string into here somehow.
}

As you can see, I need to get string into processData somehow. I can't make a global variable because string is different for every ajax request. So, the question is, how do I bind string to my ajax request so that I can access it from processData?

正如您所看到的,我需要以某种方式将字符串输入processData。我不能创建一个全局变量,因为每个ajax请求的字符串都不同。所以,问题是,如何将字符串绑定到我的ajax请求,以便我可以从processData访问它?

I really don't want to have to append string to the query and have the server return it, but if this is my only option, I have no choice.

我真的不想在查询中附加字符串并让服务器返回它,但如果这是我唯一的选择,我别无选择。

Thanks in advance.

提前致谢。

3 个解决方案

#1


5  

try in this way:

试试这种方式:

while (...) {

    var str = 'something that changes for each ajax request.';

    (function(_str) {
        $.ajax({'type': 'GET','dataType': 'json', 'url': 'get_data.php'})
         .done(function(data) {
            processData(data, _str);
         });
    }(str));
}

function processData(data, str) {
  console.log(data, str);
}

and no global variables were used :)

并没有使用全局变量:)

#2


2  

var string='something that changes for each ajax request.';
// Use a closure to make sure the string value is the right one.
(function() {
    // Store the "string" context
    var that = this;
    $.ajax({
        'type': 'GET',
        'dataType': 'json',
        'url': 'get_data.php'
    }).done(
        $.proxy( processData, that )
    );
}(string));

function processData( data ) {
    this.string === 'something that changes for each ajax request.' // true
}

$.proxy is the jQuery version (cross-browser) of .bind().

$ .proxy是.bind()的jQuery版本(跨浏览器)。

You could add an argument as @Joel suggested (in his deleted answer), but the less arguments the better.

您可以添加@Joel建议的参数(在他删除的答案中),但参数越少越好。

#3


0  

$(document).bind("ajaxSend",function(){
    $("#ajax_preloader").show();
}).bind("ajaxComplete",function(){
    $("#ajax_preloader").hide();
});

#1


5  

try in this way:

试试这种方式:

while (...) {

    var str = 'something that changes for each ajax request.';

    (function(_str) {
        $.ajax({'type': 'GET','dataType': 'json', 'url': 'get_data.php'})
         .done(function(data) {
            processData(data, _str);
         });
    }(str));
}

function processData(data, str) {
  console.log(data, str);
}

and no global variables were used :)

并没有使用全局变量:)

#2


2  

var string='something that changes for each ajax request.';
// Use a closure to make sure the string value is the right one.
(function() {
    // Store the "string" context
    var that = this;
    $.ajax({
        'type': 'GET',
        'dataType': 'json',
        'url': 'get_data.php'
    }).done(
        $.proxy( processData, that )
    );
}(string));

function processData( data ) {
    this.string === 'something that changes for each ajax request.' // true
}

$.proxy is the jQuery version (cross-browser) of .bind().

$ .proxy是.bind()的jQuery版本(跨浏览器)。

You could add an argument as @Joel suggested (in his deleted answer), but the less arguments the better.

您可以添加@Joel建议的参数(在他删除的答案中),但参数越少越好。

#3


0  

$(document).bind("ajaxSend",function(){
    $("#ajax_preloader").show();
}).bind("ajaxComplete",function(){
    $("#ajax_preloader").hide();
});