Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax call getting canceled by browser

I am using the Prototype JS framework to do Ajax calls. Here is my code:

new Ajax.Request( '/myurl.php', {method: 'post', postBody: 'id='+id+'&v='+foo, onSuccess: success, onFailure: failed} );

function success(ret) {
console.log("success",ret.readyState, ret.status);
}
function failed(ret) {
console.log("failed",ret.readyState, ret.status);
}

Most of the time, this works fine and the success function is called with a status code of 200. About 5% of the time on Safari the success function is called with a status code of 0. In this case, when I look in the Network tab of the web inspector, the ajax call is listed with a status of "canceled". I can confirm with server logs, that the request never hit the server. It's as if the ajax request was immediately canceled without even trying to connect to the server. I have not found any reliable way to reproduce this, it seems to be random. I do it 20 times and it happens once.

Does anyone know what would cause the ajax call to get canceled or return a status code of 0?

like image 789
Jake Avatar asked Sep 21 '11 20:09

Jake


1 Answers

The cause may be the combination of http server and browser you are using. It doesn't seems like an error of the PrototypeJS library.

Multiple sources states that keep-alive parameter of the HTTP connection seems to be broken in Safari (see here, here or here). On Apache, they recommend adding this to the configuration:

BrowserMatch "Safari" nokeepalive

(Please check the appropriate syntax in your server documentation).

If Safari handles badly HTTP persistent connections with your server, it may explain what you experiences.

If it's not too complex for you, I would try another HTTP server, there are plenty available on every OS.

We lack a bit of information to answer fully your answer, though. The server issue is a lead but there may be others. It would be nice to know if it does the same thing in other browsers (Firefox with Firebug will display this kind of information, Chrome, Opera and IE have development builtin toolboxes). Another valid question would be how often you execute this AJAX request per second (if relevant).

like image 186
Soravux Avatar answered Oct 22 '22 08:10

Soravux