开发者

Get url path in Ajax request's onSuccess function

Following is my code for Ajax request:

 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
 开发者_JS百科    }
 }

So, I am performing multiple ajax request in loop, Request is working perfect. But I want value of variable(SOME-VARIABLE) portion of request url in respective callback. As multiple-request are performed, in callback I could not get the the actual value of variable(it is replaced by subsequent loop iteration) Is there any way that I could access request-url-path in response-object of Onsuccess function.

Thank you.


Curly brackets do not have scope in javasrcipt, but functions do. So wrap in-brackets code by function:

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


You can store SOME-VARIABLE as a property in your request:

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

EDIT: Got this working using Extjs 3.1 and 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜