最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - JQuery .ajax timeout increase not working - Stack Overflow

programmeradmin8浏览0评论

I have a webpage where the javascript calls the PHP server using a POST request through the .ajax() function. The PHP server in turn calls an external third party API to submit a text analysis job. Once the text analysis job is processed, my PHP server queries the results from the API. However, the third party REST API does not provide any way to query the status of the job. So after I submit a job I put my program to sleep for about a minute and then query the results. However, sometimes my results are inplete. I tried setting the sleeping time to be large but setting it to be longer than a minute seems to make the initial POST request from the Javascript to the PHP time out. Setting the ajax timeout parameter to be high doesn't help. Does anyone have suggestions on how this can be worked out? Any help is really appreciated.

The ajax request looks like this:

function callServer(params, success) {
    var url = "ie.php";
    $.ajax({
       type: 'POST',
       url: url,
       data: params,
       timeout: 60000000, //time out parameter doesn't work
       dataType: "json",
       success: function(result, textStatus) {
                    console.log(JSON.stringify(result, null, '  '));
            if (success) success(result);
       },
       error: function(xhr, textStatus, errorThrown) {
                    var text = 'Ajax Request Error: ' + 'XMLHTTPRequestObject status: ('+xhr.status + ', ' + xhr.statusText+'), ' +
                                            'text status: ('+textStatus+'), error thrown: ('+errorThrown+')';
                    console.log('The AJAX request failed with the error: ' + text);
                    console.log(xhr.responseText);
                    console.log(xhr.getAllResponseHeaders());
       }
    });
}

The error looks like this:

The AJAX request failed with the error: Ajax Request Error: XMLHTTPRequestObject status: (0, error), text status: (error), error thrown: ()

I have a webpage where the javascript calls the PHP server using a POST request through the .ajax() function. The PHP server in turn calls an external third party API to submit a text analysis job. Once the text analysis job is processed, my PHP server queries the results from the API. However, the third party REST API does not provide any way to query the status of the job. So after I submit a job I put my program to sleep for about a minute and then query the results. However, sometimes my results are inplete. I tried setting the sleeping time to be large but setting it to be longer than a minute seems to make the initial POST request from the Javascript to the PHP time out. Setting the ajax timeout parameter to be high doesn't help. Does anyone have suggestions on how this can be worked out? Any help is really appreciated.

The ajax request looks like this:

function callServer(params, success) {
    var url = "ie.php";
    $.ajax({
       type: 'POST',
       url: url,
       data: params,
       timeout: 60000000, //time out parameter doesn't work
       dataType: "json",
       success: function(result, textStatus) {
                    console.log(JSON.stringify(result, null, '  '));
            if (success) success(result);
       },
       error: function(xhr, textStatus, errorThrown) {
                    var text = 'Ajax Request Error: ' + 'XMLHTTPRequestObject status: ('+xhr.status + ', ' + xhr.statusText+'), ' +
                                            'text status: ('+textStatus+'), error thrown: ('+errorThrown+')';
                    console.log('The AJAX request failed with the error: ' + text);
                    console.log(xhr.responseText);
                    console.log(xhr.getAllResponseHeaders());
       }
    });
}

The error looks like this:

The AJAX request failed with the error: Ajax Request Error: XMLHTTPRequestObject status: (0, error), text status: (error), error thrown: ()

Share Improve this question asked Aug 28, 2013 at 1:12 radhikaradhika 5342 gold badges7 silver badges15 bronze badges 5
  • Why do you think this is caused by your timeout not working? Sounds like ie.php is the one returning an error. What do your error logs say? – ChunkyBaconPlz Commented Aug 28, 2013 at 1:15
  • Everything works if I reduce the wait time to less than 60 seconds. I tried to change the php.ini so that the max_execution_time is longer but that didn't help either. – radhika Commented Aug 28, 2013 at 1:59
  • What do the web server errors logs say on the box that hosts ie.php? – ChunkyBaconPlz Commented Aug 28, 2013 at 2:04
  • if you run ie.php in browser, what is the error ? – Michael B. Commented Aug 28, 2013 at 4:25
  • there is no error in the server logs.. – radhika Commented Aug 28, 2013 at 16:13
Add a ment  | 

3 Answers 3

Reset to default 6

There are multiple places in the whole process with timeout settings:

  1. Browser / Ajax call
  2. PHP has its own: ini_set('max_execution_time', '1800'); or set_time_limit(1800);
  3. Apache itself has a max run time: Timeout apache2.conf
  4. Possibly your REST API - however you are calling it

The best case scenario in my opinion is to change from a blocking / synchronous strategy to an asynchronous strategy:

  1. Use ajax to send request from client to your server
  2. Run REST API from your server asynchronously (look up asynchronous curl or node.js)
  3. Client uses ajax to periodically query your server for status (maybe every 10 secs)

This makes the whole process asynchronous and doesn't require you to wait or block while the processing takes place

Can you do this?

function callServer(params, success) {
    var url = "ie.php";
    setTimeout(function(){
        $.ajax({
           type: 'POST',
           url: url,
           data: params,
           dataType: "json",
           success: function(result, textStatus) {
                        console.log(JSON.stringify(result, null, '  '));
                if (success) success(result);
           },
           error: function(xhr, textStatus, errorThrown) {
                        var text = 'Ajax Request Error: ' + 'XMLHTTPRequestObject status: ('+xhr.status + ', ' + xhr.statusText+'), ' +
                                                'text status: ('+textStatus+'), error thrown: ('+errorThrown+')';
                        console.log('The AJAX request failed with the error: ' + text);
                        console.log(xhr.responseText);
                        console.log(xhr.getAllResponseHeaders());
           }
        })
    }, 200);
}

To fix timeout errors like these you need to set the timeout length in 3 or 4 different places:

  1. Jquery itself (it can be done with the timeout parameter like you did, or globally with the $.ajaxSetup function)

  2. PHP by setting max_execution_time in php.ini

  3. Your web server (different for Apache and IIS. For example, in IIS, if you're running PHP via FastCGI, you have to set Idle Timeout, Request Timeout, and Activity Timeout in your FastCGI settings)

  4. Possibly your browser itself, though in the past this has not been necessary for me

If you set a timeout in all these places, it should work. For me, I had done all the other steps like you but the problem was not fixed until I set the Activity Timeout setting in my FastCGI settings.

发布评论

评论列表(0)

  1. 暂无评论