How to make Ajax request synchronous in jQuery?


Is there any way to make Ajax request Synchronous in jQuery/javascript?

I am trying to fetch a data from the server while loading the <div> data and depending on that data, i will fetch another query, which can be asynchronous but the first one should be synchronous as data will be parsed after the success of the call.

Code:

   $.ajax({
           url: "/Controller/Action",
           data: "sometext="+valueSelected,
           type: "GET",
               success: function (resp) {
                 if (resp == 1) {
                   //fetch new data calling ajax again
                 } else {
                   // don't fetch
                 }
               },
               error: function (e) {
                 console.log(e);
               }
         });

Asked by:- pika
2
: 239 At:- 8/1/2017 12:33:45 PM
jQuery javascript ajax-synchronous






2 Answers
profileImage Answered by:- manish

jQuery ajax request can be made Synchronous using it's settings

async: false

which is true by default, setting async:false will make it synchronous

Note: Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation.Synchronous requests may temporarily lock the browser, disabling any actions while the request is active

read more about it on jQuery.Ajax()

So, it should be like

 $.ajax({
        url: '/example',
        async: false
    });
1
At:- 8/1/2017 3:04:21 PM


profileImage Answered by:- vikas_jk

You can simply do something like this, Set ajax to synchronous before the ajax call, and then reset it after your ajax call:

$.ajaxSetup({async: false});

$ajax({ajax call....});

$.ajaxSetup({async: true});

 

0
At:- 8/1/2017 3:24:32 PM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use
Amazon Deals

Jobs from Indeed