2014-03-19 26 views
2

我有這樣的代碼:如何讓jQuery忽略空的服務器響應?

$.ajax({ type: "POST", 
    url: theRightUrl, 
    data: whatToPost, 
    logFunction: whatever, 
    suppressSuccessLogging: !0, 
    dataType: "html" 
    }); 

並且當所述服務器返回HTTP 204和空響應的Firefox發出「無元素找到」錯誤。根據jQuery manualcontents參數,我可以用它來啓用對響應的自定義處理。

我很樂意迴應被忽略。

如何使用contents參數來實現?

回答

1

我不認爲contents會幫助你,因爲你沒有解析的迴應。只要你沒有進行跨域請求,你所需要的只是一個error處理程序,它什麼都不做。

$.ajax({ type: "POST", 
    url: theRightUrl, 
    data: whatToPost, 
    logFunction: whatever, 
    suppressSuccessLogging: !0, 
    dataType: "html", 
    error: function() { 
     // This is still supported, but deprecated, you should 
     // consider using $.Deferred instead 
    } 
}); 

更妙的是使用$.Deferred對象

1

也許這可以幫助;

$.ajax({ 
    statusCode: { 
    204: function() { 
     alert("Sorry no response "); 
    } 
    } 
}); 
+0

很酷,但我可以用'contents'來指定一個「轉換器」嗎? – sharptooth

+0

據我所知,內容用於指定自定義類型。我不確定特定的轉換器。它應該爲你的錯誤工作,我認爲。 – Cracker