13

I am using the following ajax get function to check the status of a user. Is it possible to set async: false with this shorthand method? If not how can this be changed so that I can add async false?

$.get("/submit/checkuser/", function (userStatus) {
    if (userStatus == 'Disabled') { // check if user if disabled                    

    } else if (userStatus == 'Deleted') { // check if user if deleted

    } else {

    };
}).fail(function () {                
    connectionError();
});

5 Answers5

23

The only way to make a synchronous $.get call is to set ajaxSetup to synchronous:

jQuery.ajaxSetup({async:false});

This is considered bad form though, since it affects every ajax call made by JQuery on the page. I would suggest just biting the bullet and using the more elaborate jQuery.ajax syntax.

See How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request? for a fuller discussion.

Community
  • 1
  • 1
Stryner
  • 7,288
  • 3
  • 18
  • 18
  • 2
    can we just reset the setup back to `async:true` after the `$.get`? – Dariel Pratama Jul 27 '16 at 04:37
  • @DarielPratama You could. Since Javascript is single-threaded, that should work without any issues. – Stryner Jul 27 '16 at 04:46
  • 1
    I upvoted this answer, though people should know that at least with version 3.1, you get a warning "Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/." in the console every time you make a synchronous ajax call. – Dan Goodspeed Oct 19 '16 at 22:01
  • This is bad because it sets async to false for every request afterwards as well, which might not be desired. – kjdion84 Aug 18 '17 at 14:07
11

This might work for you

$.ajax({
    url : "/submit/checkuser/",
    type : "get",
    async: false,
    success : function(userStatus) {
       if (userStatus == 'Disabled') { // check if user if disabled                    
       } else if (userStatus == 'Deleted') { // check if user if deleted
       } else {
       };
    },
    error: function() {
       connectionError();
    }
 });
Bob Tate
  • 1,381
  • 9
  • 21
  • 7
    This should not be the correct answer as per the question which emphasizes **SHORTHAND** see @Stryner answer http://stackoverflow.com/a/25489055/1226748 – Jimmy Obonyo Abor Jan 24 '16 at 17:14
7

From above though, I use it like this

jQuery.ajaxSetup({async:false});

  // my code 

jQuery.ajaxSetup({async:true});
Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87
3

Try this.

$.get('/your/url/dot/com', { async: false }, function(data){
    globalBaseURL = data;
});
Jp Serame
  • 101
  • 1
  • 8
  • There doesn't appear to be a form of [$.get()](http://api.jquery.com/jQuery.get/) that accepts an object for options as the second parameter... it appears to only have the two signatures - i.e.`jQuery.get( url [, data ] [, success ] [, dataType ] )` and `jQuery.get( [settings ] )` so with your example, `{ async: false }` would be treated as data sent to the request... – Sᴀᴍ Onᴇᴌᴀ Aug 01 '17 at 16:09
0

Use jQuery.ajaxSetup({async:false}); because $.get() is Jquery shorthand method with syntax jQuery.get( url [, data ] [, success ] [, dataType ] ) and to make changes to setting there is jQuery.get( [settings ] )

R15
  • 13,982
  • 14
  • 97
  • 173
Subhash Diwakar
  • 199
  • 2
  • 12