2

I am trying to set a boolean value inside StatusCode of an ajax call which I could re-use later for some other work. This is what I tried:

    window.flag = false; // This is the flag I am trying to set inside Status Code 200

    $(document).ready(function() {
      $("#btnSubmit").click(function(){
         jQuery.ajax({
            'url': 'http://localhost:8080//Test/abc/',
            dataType : 'jsonp',
            crossDomain:true,
            async:false,
            statusCode: {
              200: function() {
                alert('Success'); // This popup is displayed
                flag = true; // Here I am trying to set the flag to TRUE
                alert(flag); // Here I am getting TRUE in alert
              }
            }
          });
          // But here the flag value is coming as FALSE; I am expecting TRUE value
          if(flag == false){ 
            alert('Failure');
          }
      });
    });

The alert pop-up is displaying 'Success' inside the StatusCode callback, the value of flag is set to true and the alert for flag is displaying TRUE.

BUT when I try using it inside the if condition check, the value is coming as False.

Please let me know how to retrieve the correct value for the flag.

user182944
  • 7,897
  • 33
  • 108
  • 174

1 Answers1

4

From the documentation:

Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation

So, even though you set the async flag to false it is actually performed asynchronously, and by the time your flag check is occurring, the request hasn't resolved yet.

See jQuery.Ajax

haim770
  • 48,394
  • 7
  • 105
  • 133
  • How to resolve this issue then? For CORS, I need to have `jsonp` and wont be able to remove it. Please advice. – user182944 Jun 07 '15 at 07:47
  • @user182944, Why did you opt for non-async in the first place? – haim770 Jun 07 '15 at 07:48
  • Add a success handler – roland Jun 07 '15 at 07:49
  • http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – Quentin Jun 07 '15 at 07:49
  • @haim770 I was trying if it works with async or not; hence added it. – user182944 Jun 07 '15 at 07:50
  • @user182944, Well, apparently it won't work. So, go `async` and check your `flag` after the promise has been resolved. – haim770 Jun 07 '15 at 07:51
  • can you please post some sample code for this? I tried a lot of options and now feel fairly confused. Please provide some sample code snippet on which I can work on. – user182944 Jun 07 '15 at 07:51
  • @user182944, You already provided a callback function as part of your `{ 200: function()` status code. If you need some code to run only *after* the request has succeeded, this is the place. See http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call for possible options and common patterns. – haim770 Jun 07 '15 at 07:54
  • and for unsuccessful request, if I need to display an error message then? – user182944 Jun 07 '15 at 07:55
  • "Unsuccessful request" can be represented as many Http status codes (from 404 to 500 etc). So you better go with something like `{ success: function(res) { ...}, error: function(jqXhr) {...} }`. – haim770 Jun 07 '15 at 07:57
  • and what if the request is Aborted? In that case there wont be any response code right? – user182944 Jun 07 '15 at 15:32
  • It depends. But, if you'll specify an `error` handler, it would be invoked for aborted requests as well. – haim770 Jun 07 '15 at 15:35