如何處理 jQuery AJAX 錯誤?
處理 jQuery AJAX 錯誤。ajaxError( callback ) 方法附加一個函式,以便在 AJAX 請求失敗時執行。這是一個 AJAX 事件。
以下是此方法使用的所有引數的說明 −
- callback − 要執行的函式。XMLHttpRequest 和用於該請求的設定作為引數傳遞給此函式。如果在處理請求時發生異常,則傳遞第三個引數,即異常物件。
以下示例演示了此方法的用法
<html>
<head>
<title>Handling errors in jQuery</title>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#driver").click(function(event){
/* Assume result.text does not exist. */
$('#stage1').load('/jquery/result.text');
});
$(document).ajaxError(function(event, request, settings ){
$("#stage2").html("<h1>Error in loading page.</h1>");
});
});
</script>
</head>
<body>
<p>Click on the button to load result.text file:</p>
<div id = "stage1" style = "background-color:blue;">
STAGE - 1
</div>
<div id = "stage2" style = "background-color:blue;">
STAGE - 2
</div>
<input type = "button" id = "driver" value = "Load Data" />
</body>
</html>
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP