I am making the jQuery AJAX jsonp asynchronous request from my browser (IE, Mozilla, Chrome) to my Java app. If my response does not come with in 4 minutes, request gets timed out on all browser. In case of time out, IE automatically fires new request and Mozilla simply terminates that request. I do not want the request to time out (I mean it should wait until the response does not come from server) Is there a way I can disable the timeout for jQuery AJAX JSONP request?
Some sites suggest setting timeout value too large as workaround for this which I do not want to use because still it will be time out though after a long duration.
Another option, which is arguably not as advanced as websockets, is to poll an address for the result. Consider the following scenario:
You need to display a report on screen, but it takes a long time to load. You can send the initial request to the server. The server then initiates the report, and returns a uri which the client can poll for the result. The client then polls that uri and will (possibly) get a 404 until the report is done. When the report is done, it will be accessible at the uri, and you can then display it to the browser.
I recommend you not trying to kick against the goads of browser timeouts and fancy javascript circumvention. Even if you get it to work sometimes, it will be error prone as people use different browsers (even mobile these days). Having a robust solution goes along way in keeping things simple and maintainable.
As @BalusC mentioned, web sockets are great for this kind of stuff. If you're willing change your paradigm a little and have time on your hands, then its a great solution. The web will most certainly go this way. However, if you're under time pressure, it might not be the best solution in the short term.
I don't think there is anyway you can let the requests time out and then handle that gracefully, as you already noticed it's up to the browser what exactly happens, you have almost no control.
However, the solution is fairly simple i think, just don't let your requests timeout.
With little effort you can change your jsonp callback handler in your java application to notify the client that a timeout period has expired and that it should do the request again.
I created a fully functional working example to show you exactly what i mean.
This example is in nodejs, but the server side implementation should really be kept minimal i think. It's really more about what the client side code does.
Demo:
http://jsonp-timeout.herokuapp.com/
Source:
https://github.com/helmus/jsonp-timeout/blob/master/public/index.html
This is really all the relevant code:
Basically you put your jsonp call inside a function so you can reuse it. If your handler then receives a "timeout" response, it can easily do the request again until your server decides other actions are relevant.
$(function(){
$("#call").on("click", function(){
var makeJsonpCall = function(){
return $.ajax({
url: "rpc.js",
dataType: "jsonp"
});
};
var handler = function(data){
if (data === "timeout") {
console.log("a timeout occured, forwarding your request");
makeJsonpCall().done(handler);
return;
}else{
console.log("request completed");
}
};
makeJsonpCall().done(handler);
});
});
I think this approach is certainly robust enough and allows you to keep your interfaces with minimal change and it will off-course still work with cross domain jsonp if the endpoint supports it.
You can change the timeout period to 2 or 3 minutes (server side), but i wouldn't make it much longer then that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With