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

Javascript 如何将变量绑定到jquery ajax请求?,javascript,jquery,ajax,data-binding,jquery-deferred,Javascript,Jquery,Ajax,Data Binding,Jquery Deferred,这是不言自明的: 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. } 如您所

这是不言自明的:

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.
}
如您所见,我需要以某种方式将
string
放入
processData
。我无法生成全局变量,因为每个ajax请求的
string
都是不同的。因此,问题是,如何将
string
绑定到ajax请求,以便从
processData
访问它

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

提前谢谢

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
}
是的jQuery版本(跨浏览器)

您可以按照@Joel的建议(在他删除的答案中)添加一个参数,但是。

请尝试以下方法:

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

而且没有使用全局变量:)

只是一个想法:如果
string
中的一个值,而
中发生了变化,那么如何确保将正确的
string
值传递给
processData
函数(而不是最后一个)?嗯,我想使用闭包来确保这是正确的方法。编辑:@F.Calderan。
$(document).bind("ajaxSend",function(){
    $("#ajax_preloader").show();
}).bind("ajaxComplete",function(){
    $("#ajax_preloader").hide();
});