我正在測試jQuery ajax請求的錯誤回調,我有一個問題。下面是我的Ajax請求jQuery ajax請求錯誤回調
$.ajax({
type:'get',
url:'non-existant-file.php',
data: { newFile : $('#name').val() },
beforeSend: function() {
// make nice image to show the request is running
},
success: function() {
// show results of successfull request
},
error: function(jq,status,message) {
alert('A jQuery error has occurred. Status: ' + status + ' - Message: ' + message);
}
});
該文件不存在,測試時引發錯誤的唯一目的。我期望它返回一個404作爲狀態,並且'File not found'作爲消息。 ATLEAST,這就是我所理解的是,當我讀了docs:
Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn.
但是,我得到的警告沒有像我所期待的:
A jQuery error has occurred. Status: error - Message: undefined
我是理解的文檔錯誤還是我只是不正確?
你在的情況下,應用程序中有一個錯誤頁的URL不存在? – davids
像一個默認的404頁面?那就不要。我不。在鉻錯誤控制檯它只是拋出'GET http://localhost/km/admin/createFile.php?newFile = 404(未找到)',我會想到的是,將返回ajax錯誤的方式。 – Albert