AJAX and 200 OK messages: Are they required to complete a server side call?

We’re developing a tracking system that makes 3 ajax calls in row within the WordPress environment using the WordPress AJAX API and on the 3rd call it refreshes the page.

I’m noticing that sometimes on of my data entry attempts is failing on the back end and I am assuming (possibly incorrectly) that this is happening because our final Ajax call completes before the prior one gets the 200 OK clear, and breaks the connection, which means the data never gets stored.

Read More

Now I am under the impression that as soon as an jQuery.ajax({type: ‘POST’ call is issued I could immediately close the page and still my server side that I posted to would receive, and process the data, regardless of what happens the the parrent connection.

But my data testing is showing some data is not getting entered consistently. Am I right to think this should NOT be related to my final AJAX call finishing (and reloading the page) before a prior AJAX call clears with a 200OK?

Thanks for any help!

Related posts

Leave a Reply

1 comment

  1. Yes, If you send the ajax request to server, than you can close the page, server will complete the request(execute code).

    But there is a point you have to know, you can’t know if your request arrived to server.

    jQuery.ajax("htpp://",{type: 'POST'});
    

    This is an asynchronous request, so it will execute the code in background. Executing this line of code doesn’t mean that you have sent the request. It has some phases that works in background.

    1. jQuery will parse your code, look what you want,
    2. It will create a xmlhttprequest object, than set options.
    3. And finally it will send request the server.

    So if you close the page immediately, before the third phase, request won’t be sent.

    I recommend that you send a synchronise ajax request and wait for any response to refresh/close page. Because synchronous ajax requests may temporarily lock the browser, disabling any actions while the request is active.

    In jQuery.ajax() you must set async false to make it synchronous, like this,

      jQuery.ajax({
        url: Url,
        success: function(r) {},
        async:false
      });