获取Ajax请求的功能的onSuccess URL路径

问题描述:

以下是我对Ajax请求的代码:在循环获取Ajax请求的功能的onSuccess URL路径

for(some loop) { 
var SOME-VARIABLE = value; 
new Ajax.Request(SERVER-PATH + SOME-VARIABLE, { 
    asynchronous: true, 
    method: 'get', 
    onSuccess: function(response) { 
    //here I require SOME-VARIABLE 
    } 
} 

所以,我执行多个Ajax请求,请求正在完善。但是我想在各自的回调中请求url的变量(SOME-VARIABLE)部分的值。当多重请求被执行时,在回调函数中,我无法获得变量的实际值(它被后续的循环迭代所取代)有什么方法可以访问Onsuccess函数的响应对象中的request-url-path。

谢谢。

大括号在javasrcipt中没有作用域,但是函数可以。因此,在包装,括号中的代码按功能:

for(some loop) { 
    var SOME-VARIABLE = value; 
    (function(url) { 
    new Ajax.Request(SERVER-PATH + url, { 
     asynchronous: true, 
     method: 'get', 
     onSuccess: function(response) { 
     //here You can access url 
     } 
    } 
    }) (SOME-VARIABLE); 
+0

十分感谢,这个工作! – Shanta

您可以将某些变量存储为您的要求的属性:

new Ajax.Request(SERVER-PATH + SOME-VARIABLE, { 
    asynchronous: true, 
    method: 'get', 
    options: {SOME-VARIABLE: SOME-VARIABLE}, 
    onSuccess: function(response, obj) { 
     alert(obj.options.SOME-VARIALBE); 
    } 
} 

编辑: 得到这个使用ExtJS的3.1和4.0.2工作:

var myOption = 'asdf'; 
Ext.get('myButton').on('click', function(){ 
    Ext.Ajax.request({ 
     url: 'http://jsfiddle.net/', 
     options: {myOp: myOption }, 
     success: function(res, obj) { 
      alert(obj.options.myOp); 
     }, 
     failure: function(res, obj) { 
      alert(obj.options.myOp); 
     } 
    }); 
}); 
+0

看似简单的解决方案,但没有奏效,我发现obj undefined。 – Shanta

+0

你使用的是什么Ext版本? –

+0

是的,这是版本问题,我使用的是Ext2.0.2,我不能升级它,因为这个lib是另一个(第三方)框架的一部分。谢谢你纠正我。 – Shanta