0

I need to make an ajax request and then process some data returned from server. I'd like to make this request earlier then document is ready. But i will be able to process data only after document is ready. I've got an idea to make something like this:

$.when($(document).ready(),
       $.ajax(/*some request*/)).done(function(){//processing data from the server});

How will it work? or is there any other options to get the result i want

Bergi
  • 630,263
  • 148
  • 957
  • 1,375

3 Answers3

0

Try using: $( document ).ajaxComplete(function() {}

0

Without using async helper libraries, You can use some flags and a helper function, not the most elegant but it works

var docReady = false;
var responseReady = false;
var tryEvaluateResponse = function() { 
    if(docReady && responseReady) {/*processing data from the server*/}
};
$(document).ready(function() {
    docReady = true;
    tryEvaluateResponse();
});
$.ajax(/*some request*/)).done(function() {
    responseReady = true; 
    tryEvaluateResponse()
});
lyjackal
  • 3,984
  • 1
  • 12
  • 25
0

No, this won't work, as $(document).ready() doesn't return a promise. You can however do

var request = $.ajax(/*some request*/);
$(document).ready(function() {
    request.done(function(){
        //processing data from the server
    });
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375