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.

0

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

12 Comments

How to resolve this issue then? For CORS, I need to have jsonp and wont be able to remove it. Please advice.
@user182944, Why did you opt for non-async in the first place?
Add a success handler
@haim770 I was trying if it works with async or not; hence added it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.