Async false for shorthand AJAX get request

前端 未结 5 1074
梦毁少年i
梦毁少年i 2020-12-11 02:00

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

相关标签:
5条回答
  • 2020-12-11 02:33

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

    0 讨论(0)
  • 2020-12-11 02:37

    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();
        }
     });
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-11 02:54

    From above though, I use it like this

    jQuery.ajaxSetup({async:false});
    
      // my code 
    
    jQuery.ajaxSetup({async:true});
    
    0 讨论(0)
  • 2020-12-11 02:55

    Try this.

    $.get('/your/url/dot/com', { async: false }, function(data){
        globalBaseURL = data;
    });
    
    0 讨论(0)
提交回复
热议问题