emilly
emilly

Reputation: 10540

How to disable timeout for AJAX JSONP request?

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.

Upvotes: 3

Views: 3000

Answers (3)

Fallenreaper
Fallenreaper

Reputation: 10704

My way involves setting up a server, a client, and a database.

Create a database which stores a unique hash key with a time apender. That is the client request key. It contains either a datablock of what you are looking for datawise, OR a boolean to know if that request is done.

Server side: When a client calls this request, store its unique key and start to process the event. Until the process is done the second column will have a false. When it is completed, you flip that table to true.

Client: ping the server with the ajax request, then in the success call of that request use timeout and create a chain of requests over time which will check the database to see if the process is done. On the inner request, the timeout would need to be like once every 4 minutes or whatever you like, but when it returns success, it will have the desired data and you would want to remove the timer. If it doesnt return true, then the error function would be called, just doing nothing.

This doouble request system would allow you to fire a request and monitor it over time. When it completes you will have what you are looking for. This system would remove the time out because it is firing 1 request, and then will fire checkup requests over time.

This is how i would resolve it such that there is no timeout. No hanging processes pending data fetching, etc.

Edit: you would want to them run a cleanup script which would remove that row from the database table you did, and all that other stuff which is relevant to not wasting space.

Upvotes: 1

Willem D'Haeseleer
Willem D'Haeseleer

Reputation: 20190

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.

Upvotes: 2

Gevious
Gevious

Reputation: 3252

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.

Upvotes: 2

Related Questions