最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Access javascript variable inside ajax success - Stack Overflow

programmeradmin5浏览0评论
var flag = false; //True if checkbox is checked
$.ajax({
    ... //type, url, beforeSend, I'm not able to access flag here
    success: function(){
        // I'm not able to access flag here
    }
});

Inside ajax, if I try to access flag it says it is not defined. How can I use it inside ajax function?

Any Idea?

Both flag and ajax is body of a function. Nothing else is present inside that function.

var flag = false; //True if checkbox is checked
$.ajax({
    ... //type, url, beforeSend, I'm not able to access flag here
    success: function(){
        // I'm not able to access flag here
    }
});

Inside ajax, if I try to access flag it says it is not defined. How can I use it inside ajax function?

Any Idea?

Both flag and ajax is body of a function. Nothing else is present inside that function.

Share Improve this question edited Sep 14, 2020 at 8:37 drhelado 336 bronze badges asked Jun 9, 2015 at 14:03 GibbsGibbs 23k14 gold badges83 silver badges145 bronze badges 15
  • 1 because, flag will be out of scope once the ajax call triggers the success call back. – Rajaprabhu Aravindasamy Commented Jun 9, 2015 at 14:04
  • 1 @RajaprabhuAravindasamy No, success doesn't magically remove variable from context. Here the flag variable is even not accessible from ajax option so OP is setting it outside of ajax method scope$ – A. Wolff Commented Jun 9, 2015 at 14:14
  • 1 Where exactly are you defining flag? I did a test and it worked fine. Closures means that if flag and the $.ajax call are at the same scope then flag will be available (and it is). There's some missing code in your question... – fdomn-m Commented Jun 9, 2015 at 14:15
  • 2 I get the feeling this isn't your actual code. This should work fine jsfiddle.net/djgdggay – CodingIntrigue Commented Jun 9, 2015 at 14:16
  • 3 @GopsAB You misunderstand me. I'm not saying this isn't your own code, I'm saying that the code you posted isn't representative of the problem you describe. – CodingIntrigue Commented Jun 9, 2015 at 14:19
 |  Show 10 more comments

2 Answers 2

Reset to default 15

You have access to the variable if you make it by reference. All objects in Javascript are referenced values, just the primitive values aren't (such as: int, string, bool, etc...)

So you can either declare your flag as an object:

var flag = {}; //use object to endure references.

$.ajax({
    ... //type, url, beforeSend, I'm not able to access flag here
    success: function(){
        console.log(flag) //you should have access
    }
});

Or force the success function to have the parameters you want:

var flag = true; //True if checkbox is checked

$.ajax({
    ... //type, url, beforeSend, I'm not able to access flag here
    success: function(flag){
        console.log(flag) //you should have access
    }.bind(this, flag) // Bind set first the function scope, and then the parameters. So success function will set to it's parameter array, `flag`
});

Here is one way. The beforeSend callback option gives you access to both the jqXHR object and the ajax settings object.

You won't be able to use caturl in the append of error, as it will not be in synch with the request throwing error.

 $.ajax({
    /* url, data ...& other opts*/
    beforeSend:function( jqXHR, settings){
        /* add url property and get value from settings (or from caturl)*/
         jqXHR.url= settings.url;
   },
   /* error to be deprecated in jQuery 1.8 , superseded by "fail" */
   error: function(jqXHR, , textStatus, error){
       var url=jqXHR.url;
     /* replace caturl with url in your append */ 
     $('#showdata').prepend("ERROR : '" + error + "' trying to access: " + url + "</br>");
   }
发布评论

评论列表(0)

  1. 暂无评论