2013-08-28 44 views
1

我有一個網頁,JavaScript通過.ajax()函數使用POST請求調用PHP服務器。 PHP服務器反過來調用外部第三方API來提交文本分析作業。一旦處理了文本分析作業,我的PHP服務器就會查詢API的結果。但是,第三方REST API不提供任何方式來查詢作業的狀態。所以在我提交工作之後,我把我的程序休眠了大約一分鐘,然後查詢結果。但是,有時我的結果不完整。我嘗試將睡眠時間設置得很大,但將其設置爲超過一分鐘似乎使得從Javascript到PHP的初始POST請求超時。將ajax超時參數設置爲高無助於解。有沒有人有如何解決這個問題的建議?任何幫助真的很感激。JQuery .ajax超時增加不起作用

Ajax請求如下:

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()); 
     } 
    }); 
} 

錯誤看起來是這樣的:

Ajax請求失敗,出現錯誤:AJAX請求錯誤:XMLHTTPRequestObject狀態:(0,錯誤),文本狀態:(錯誤),拋出的錯誤:()

+0

爲什麼你認爲這是由於你的超時不起作用?聽起來像ie.php是一個返回錯誤。你的錯誤日誌說什麼? – ChunkyBaconPlz

+0

如果我將等待時間縮短到60秒以內,一切正常。我試圖改變php.ini,以便max_execution_time更長,但這也沒有幫助。 – radhika

+0

Web服務器錯誤日誌在主機「ie.php」的框中表示什麼? – ChunkyBaconPlz

回答

2

有在整個過程中多個地方有超時設置:

  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

在我看來,最好的情況是,從一個阻塞/同步策略更改爲異步策略:

  1. 使用AJAX來從客戶端向服務器發送請求到服務器
  2. 運行REST API異步(查找異步捲曲或node.js的)
  3. 客戶端使用Ajax定期查詢您的狀態服務器(每10秒也許)

這使得整個過程異步的,不需要你爲w在處理過程中發生或阻止

+1

我想添加另一個地方,這發生在IIS本身。如果您通過FastCGI運行IIS和PHP,請進入FastCGI設置,並使用Idle Timeout,Request Timeout和Activity Timeout(它對我來說)設置進行操作。 – dallin

0

你能做到嗎?

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); 
} 
0

要解決這樣的超時錯誤,你需要設置超時長度在3個或4個不同的地方:

  1. jQuery的本身(它可以與超時參數來完成像你這樣,或全球與$ .ajaxSetup功能)

  2. PHP受php.ini

  3. 您的Web服務器(不同設置的max_execution_time爲Apache和IIS。例如,在IIS中,如果您通過FastCGI運行PHP,則必須在FastCGI設置中設置空閒超時,請求超時和活動超時)

  4. 可能是您的瀏覽器本身,儘管過去沒有對我來說是必要的

如果你在所有這些地方設置超時,它應該工作。對於我來說,我已經完成了像您這樣的其他所有步驟,但是直到我在FastCGI設置中設置「活動超時」設置後,問題才得以解決。