14

I am not able to make ajax error callback function of after 3 seconds. I tried with timeout, but it will not switch to error callback after specified time! I am not able to get the alert Got timeout.

When I referred similar questions in this site with similar problems it didn't helped out. They all use ajax GET type. I am using jquery 1.10.1 library.

script :

$.ajax({
  type: 'POST',
  timeout: 3000,
  url : "http://mydomain/Services.asmx/Best_Scores",
  dataType: "text",
  async:false,
  crossDomain:true,
  data: "strJsonRequest="+scoredata,
  success: function (data) {
    // Success code ...
  },
  error: function (data, textStatus, errorThrown) {
    if(textStatus == "timeout") {
      alert("Got timeout");
    }
  }
});

Any solution ?

3
  • How long does the RTT take to http://mydomain/Services.asmx/Best_Scores? Maybe the url simply does not time out?
    – Amberlamps
    Jun 29, 2014 at 18:03
  • @Amberlamps : Thanks for reply, I will take 15-20 seconds !
    – byJeevan
    Jun 29, 2014 at 18:05
  • Did you try to alert something in the error function that is not in the if statement? Maybe textStatus is not "timeout"?!
    – Amberlamps
    Jun 29, 2014 at 18:07

2 Answers 2

18

Fix :

Change async : false to async: true

Reason :

A synchronous AJAX call blocks until the request has been finished. Implementing a timeout is not possible for technical reasons, because the AJAX call would have to be executed later.

If an AJAX call is executed later, the function somehow has to implement a blocking feature, to stop the code from running further after the AJAX call, and execute it again after a timeout - not possible.

2
  • Is this mentioned anywhere in the JQuery documentation? I could not find it.
    – Pardhu
    Apr 7, 2020 at 15:16
  • Not sure... But SO is the final destination for developers after all 💛
    – byJeevan
    Apr 7, 2020 at 16:23
3

Today, (in 2019) I still have this problem.

I have an ajax call too long (depending from Date Start-> Date End) php script.

and after some minutes I get error 500, async: true don't help.

The call is:

$.ajax({
    type: "GET",
    dataType: 'json',
    url: 'mymscript.php',
    contentType:'application/json;charset=UTF-8;',
    data: $('[name="myForm"]').serialize(),
    async:true,
    timeout:0,
success: function(response){
...

I resolved using: side PHP:

set_time_limit(0);
ignore_user_abort(true);

at begin of script.

echo ' ';
flush();
ob_flush();

in the middle of script (for example in main loop every day). This help to let client to don't disconnect (I think that is the main problem).

Using this I continuosly write spaces befor the final json. Fortunately jquery trim spaces before to parse the json and, in the end, the json is valid.

So I can catch response object to know if script is ended with errors or warnings.

I Hope this help somebody.

2
  • 1
    Accepted answer didn't worked for me, but this one did the job. @Mago Maverick Thank You Jan 16, 2020 at 7:28
  • This answer also worked perfectly for me in 2022, where the accepted answer didn't solve my issue. Nov 4, 2022 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.