$.get()
回調是一個成功函數,所以這絕不會出現錯誤,您可以使用$.ajax()
作爲完整版本,或者創建一個全局錯誤事件處理程序,無論哪一個更合適。
如果你想使用$.ajax()
,使用error
回調,像這樣:
$.ajax({
url: nextHref,
success: function(data) {
//do something with good data, what comes after your if statement currently
},
error: function(xhr, status, error) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
}
});
或者使用全局事件處理$().ajaxError()
這樣的:
$(document).ajaxError(function(e, xhr) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
});
或者,使用$.ajaxSetup()
添加一個error
所有請求的處理程序,如下所示:
$.ajaxSetup({
error: function(xhr, status, error) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
}
});