tags:

views:

835

answers:

1

Hi All,

I have found a problem with use JQuery timeout, i have a page with script, it's set a timeout and waiting response from server. The problem is when timeout have to perform, in short words, it "no action". I think that script not interrupt the connection with server and wait for server response, because in server side the connection is on for 10 sec (but timeout is set 5 sec). I see this problem when server work in different domain name then client, whereas when the server and client work in local this problem there isn't.

Have you a idea because this error happened or how to close the Script connection?

// client 

$.ajax({

    type: "GET",

    url: "some.php",

    data: "name=John&location=Boston",

    timeout: 5000,

    success: function(msg){

        alert( "Data Saved: " + msg );

    },
    error: function(request, errorType, errorThrown){

        alert("opppsssss .... ");
    }
});

and the server code:

//some.php

< ?

//simulate long task

sleep(10); //sleep 10 seconds

//send response

echo "some test data";

? >

Best Regards

Domenico

+3  A: 

You're doing it back-to-front by the looks of it. You've set the timeout to 5000 milliseconds (5 seconds) so if the server-side code takes longer to respond than 5 seconds it will timeout. You're doing sleep(10); which sleeps for 10 seconds, which is twice as long as the specified timeout. The way to fix this would be to do the following:

// client 
$.ajax({
    type: "GET",
    url: "some.php",
    data: "name=John&location=Boston",
    timeout: 10000,

    success: function(msg){
        alert( "Data Saved: " + msg );
    },
    error: function(request, errorType, errorThrown){
        alert("opppsssss .... ");
    }
});

And the server code:

//some.php
< ?
    //simulate long task
    sleep(5); // sleep 5 seconds!
    //send response
    echo "some test data";
? >

Hopefully that helps.

Kezzer