5

Possible Duplicate:
How can I get jQuery to perform a synchronous, rather than asynchronous, AJAX request?

I have a method that returns initialization data. It first checks the sessionStorage. If it doesn't find the data there, it makes a call to the server to get the data. Here's the code:

function getInitializationData() {

// Check local storage (cache) prior to making a server call.
// Assume HTML 5 browser because this is an Intranet application.
if (!sessionStorage.getItem("initialData")) {

    // The browser doesn't have the initialization data,
    // so the client will call the server to get the data.
    // Note: the initialization data is complex but 
    // HTML 5 storage only stores strings. Thus, the
    // code has to convert into a string prior to storage.
    $.ajax({
        url: "../initialization.x",
        type: "POST",
        dataType: "json",
        timeout: 10000,
        error: handleError,
        success: function(data) { sessionStorage.setItem("initialData", JSON.stringify(data)); } 
    });
}

// convert the string back to a complex object
return JSON.parse(sessionStorage.getItem("initialData"));
}

The problem is that the success function almost always gets executed after the method returns. How can I make the server call synchronous such that the success function must execute prior to the return statement of the getInitializationData method?

Community
  • 1
  • 1
James
  • 2,876
  • 18
  • 72
  • 116
  • 12
    This question gets asked every day... you could make it synchronous, but it's generally a bad idea. Make `getInitializationData` accept a callback or look into deferred objects (http://api.jquery.com/category/deferred-object/). Gonna write a canonical question/answer now, because at some point it's enough... – Felix Kling Jan 08 '13 at 15:14
  • 1
    have you ever read the jQuery API? what about `async:false` – Dirty-flow Jan 08 '13 at 15:16
  • 1
    http://api.jquery.com/jQuery.ajax/ -- the first result from Googling "jquery ajax synchronized" – Reinstate Monica -- notmaynard Jan 08 '13 at 15:16
  • 1
    Ironic the amount of questions the OPs have to bypass while just writing the initial question, and blatant disregard of the list to the right. ;p – Brad Christie Jan 08 '13 at 15:16
  • You can add `async: false`to your options in the `$.ajax` call and this will do the trick. The property is deprecated however and may notbe supported in future versions. – War10ck Jan 08 '13 at 15:16
  • Duplicates of async confusion: http://stackoverflow.com/q/11346691/710446 (nearly exact), http://stackoverflow.com/q/13972243/710446 (about IndexedDB rather than Ajax) – apsillers Jan 08 '13 at 15:17
  • I wonder who upvoted the question :/ – gopi1410 Jan 08 '13 at 15:22
  • OK; I will try to determine how to make this work with a callback. As Felix Kling and War10ck pointed out, the async option is deprecated. – James Jan 08 '13 at 16:42

1 Answers1

8

Use async: false

function getInitializationData() {

// Check local storage (cache) prior to making a server call.
// Assume HTML 5 browser because this is an Intranet application.
if (!sessionStorage.getItem("initialData")) {

    // The browser doesn't have the initialization data,
    // so the client will call the server to get the data.
    // Note: the initialization data is complex but 
    // HTML 5 storage only stores strings. Thus, the
    // code has to convert into a string prior to storage.
    $.ajax({
        url: "../initialization.x",
        type: "POST",
        dataType: "json",
        timeout: 10000,
        async: false,
        error: handleError,
        success: function(data) { sessionStorage.setItem("initialData", JSON.stringify(data)); } 
    });
}

// convert the string back to a complex object
return JSON.parse(sessionStorage.getItem("initialData"));
}
gopi1410
  • 6,567
  • 9
  • 41
  • 75
  • 1
    @Dirty-flow oh come on... not everyone knows that. Dude wantz sum pointz! – mraaroncruz Jan 08 '13 at 15:20
  • @Dirty-flow: tons of questions each day are closed and these types of basic questions are asked each day. Thats does not mean you do not answer when you encounter one such question. – gopi1410 Jan 08 '13 at 15:21