-3

timeout works! but when timeout I want to run a function. do you have any ideas?

$.ajax({ 
type: "GET", 
url: "xajax.php", 
timeout: 100, 
data: "name=John&location=Boston", 
success: function(msg){ alert( "Data Saved: " + msg ); } 
Igor
  • 33,276
  • 14
  • 79
  • 112
zapata
  • 61
  • 10
  • 3
    Does the server return `json`... ? – gdoron Oct 22 '12 at 07:39
  • It is hard to pinpoint the problem without the response. Most of the time the issue is bad json. Can you go to `xajax.php?result=hello` in the browser and add the response to your question? – VDP Oct 22 '12 at 07:44
  • response is a Javascript alert box "parseerror" – zapata Oct 22 '12 at 08:21

2 Answers2

1

Probably your json object response is incorrect, check it for example in: - http://jsonlint.com/

you could check it too in your browser console/network specs to see if it is in client or server side.

You could display what you are sending from server ( after create json object) in order to see if you are sending correctly.

viruskimera
  • 193
  • 16
  • i dont want to create JSON. now response is alert box "Data Saved: John" i $.ajax({ url: "xajax.php", type: "GET", // dataType: "json", data: "name=John&location=Boston", timeout: 40000, success: function(msg){ alert( "Data Saved: " + msg ); } }); – zapata Oct 22 '12 at 08:22
  • but now; timeout doesnt work! – zapata Oct 22 '12 at 08:26
  • ok, well if you are specifying dataType:json, you must receive a json object which you create in server side, this timeout will start when you make ajax call, if you are printing now your response and u get correct result, which I think is : "John" then time out wont applied, there cause you are receiving it before time ends. – viruskimera Oct 22 '12 at 08:44
  • thanks viruskimera. you are right. I decreased timeout to "100". It works. but when timeout I want to run some functions. Do you have any ideas? – zapata Oct 22 '12 at 09:52
  • You could add a error function after success one, error: function(x, t, m) { if(t==="timeout") { alert("got timeout");call function Here; } else { alert(t); } } – viruskimera Oct 22 '12 at 10:17
  • $.ajax({ type: "GET", url: "xajax.php", timeout: 100, data: "name=John&location=Boston", success: function(msg){ alert( "Data Saved: " + msg ); } error: function(x, t, m) { if(t==="timeout") { alert("got timeout"); alert("time is out"); } else { alert(t); } } – zapata Oct 22 '12 at 10:50
  • it doesnt work because my codes dont include json dataType. your error is available for json response! – zapata Oct 22 '12 at 10:51
0
$.ajax({ 
type: "GET", 
url: "xajax.php", 
timeout: 100, 
data: "name=John&location=Boston", 
success: function(msg)
    { 
    $("#result").html("here is result: "+msg);
    }
complete: function(jqXHR, textStatus)
    { 
    if (textStatus == "timeout")
        {
        $("#result").html("TIMEOUT!");
        }
    }
 });
zapata
  • 61
  • 10