2012-04-18 73 views
0

我目前正在使用phonegap編寫HTML5和Zepto.js的移動應用程序。我們的服務器在rails上使用ruby。在Playbook(測試設備)上,應用程序在此屏幕上凍結了大約20%的時間。如果發生這種情況,它將不會響應,並且頁面將在拖動時滾動(通常禁用的內容)。我們很確定這是對我們的服務器的ajax調用。這裏的電話:如何解決我的ajax調用崩潰我的移動應用程序?

$.ajax({ 
     url: myurl+ajaxData+'&callback=?', 
     dataType: 'json', 
     async: true, 
     callback: "callback", 
     success: function(body) { 
      if (body.status === "successful"){ 
       successful(); 
      } 
      else { 
       var errstring = body.status + ": " + body.result 
       console.log (errstring); 
       alert(errstring); 
      } 
     }, 
     error: function(xhr, type) { 
      var errorstring = type + ": " + xhr.status + "\n" + xhr.statusText + "\n" + xhr.responseText; 
      alert (errorstring); 
      console.log (errorstring); 
      storage.setItem("retrieved", "false"); 
     } 
    }) 

有沒有人知道可能是什麼原因造成的?

回答

1

我建議你通過如下代碼更新正確超時設置爲呼叫和誤差函數趕上什麼是問題:

$.ajax({ 
    url: myurl+ajaxData+'&callback=?', 
    dataType: 'json', 
    async: true, 
    error: function(jqXHR, strError){ 
     if(strError == 'timeout') 
     { 
      //do something. Try again perhaps? 
     } 
    }, 
    success: function(){ 
     //do something 
    }, 
    timeout:3000 
}); 

你可以看到被拋出什麼類型的錯誤通過訪問error:function(jqXHR,textStatus,errorThrown)選項的textStatus參數。選項有「超時」,「錯誤」,「中止」和「解析錯誤」。

相關問題