BaconJuice
BaconJuice

Reputation: 3779

$.post .done callback not firing but data gets posted to server

I have a simple javascript where I am POSTing to a server some data.

$.post(url,dataJSON, function() {
}).done(function(json) {
    $("#summary-modal").modal('hide');
}).fail(function(xhr, status, error) {
    var err = "<div>"+xhr.responseText+"</div>";
    $('#submit_err').append(err);
    $('#submit_err').show();
});

Now for some odd reason once the POST has been sent to the server and been accepted, it doesn't go the .done function. So in this case the modal doesn't hide.

Any ideas what might be wrong?

Thank you for reading.

Upvotes: 0

Views: 79

Answers (1)

user3310334
user3310334

Reputation:

the server does not respond with a 200, but instead in chrome dev tools it shows "pending"

It seems like your server may not be terminating the session, as in, the server receives your request, processes it, does its necessary steps (adds to a DB), but then doesn't tell your JavaScript that everything is finished and that it's time to move on.

If you yourself are writing the server, you can fix this easily just by ending the session, but specifics rely on which language your server has been written in.

In node.js, you want something like this:

response.writeHead(200, {'Content-Type': 'application/json'}); // "success" status
response.end(); // ends session

In PHP

session_write_close (); // writes all data and closes

But just look into your own language's method for doing this.

Unless you actually have some influence over the server though, you can't really do anything about this problem - maybe you can figure out a fancy method, to constantly check the responseText property of your xmlhttprequest object, but even that might not work.

Upvotes: 1

Related Questions