2010-05-19 121 views
2

我在Firefox中遇到問題(IE中不會發生這種情況),其中Javascript啓動了一個服務調用,可能需要一點時間時間加載。如果用戶在呼叫結束前導航到另一個頁面,則會調用jQuery.ajax()對象的error方法。

我正在尋找一種方式來之一:

  1. 不是有拋出的錯誤,當用戶瀏覽到另一個頁面時請求還沒有完成,或
  2. 有錯誤方法區分「真正的「錯誤和這個錯誤。

我的錯誤方法毫不奇怪地向用戶顯示一個錯誤,而當導航到另一個頁面導致錯誤時,我不想這樣做。

我的簡單測試代碼涉及以下Javascript和睡眠20秒然後返回字符串的服務。

<script type="text/javascript" language="javascript"> 
    $(document).ready(function() 
    { 
     $('#testLink').click(function() 
     { 
      $.ajax({ 
       type: "POST", 
       url: "/Services/NU.Web.NUnet.Services.UserService.asmx/LongRunningService", 
       data: "{}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (data, textStatus, xhr) 
       { 
        console.log(data); 
        console.log(textStatus); 
        console.log(xhr); 
       }, 
       error: function (xhr, textStatus, errorThrown) 
       { 
        console.log(xhr); 
        console.log(textStatus); 
        console.log(errorThrown); 
       } 
      }); 
     }); 
    }); 
</script> 

回答

3

嘗試:

<script type="text/javascript" language="javascript"> 
    $(function() { 
     $('#testLink').click(function() { 
      $.ajax({ 
       type: "POST", 
       url: "/Services/NU.Web.NUnet.Services.UserService.asmx/LongRunningService", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (data, textStatus, xhr){ 
        console.log(data); 
        console.log(textStatus); 
        console.log(xhr); 
       }, 
       error: function (xhr, textStatus, errorThrown) { 
        if (xhr.status === 500) { 
         console.log(xhr); 
         console.log(textStatus); 
         console.log(errorThrown); 
        } 
       } 
      }); 
     }); 
    }); 
</script> 
+1

真棒,非常感謝!瀏覽到另一個頁面時,狀態爲0,所以我正在檢查該值。這是一個棘手的問題,因爲我無法看到Firebug中的任何內容,因爲頁面正在重新加載:x – wsanville 2010-05-19 19:46:06

+1

歡迎您。您可以使用Firebug控制檯中的「Persist」按鈕來處理這些類型的錯誤。 – 2010-05-19 19:50:24

相關問題