jQuery中AJAX的回調


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Title</title>
</head>
<body>
<script src="jquery-1.12.4"></script>
<script>

// 原生操作中不管請求狀態碼是多少都會觸發回調
// var xhr = new XMLHttpRequest()
// xhr.open('get', 'time1.php')
// xhr.send()
// xhr.onreadystatechange = function () {
// if (this.readyState !== 4) return
// console.log(this.responseText)
// }

// 顯示 loading

$.ajax({
url: 'time.php',
type: 'get',
beforeSend: function (xhr) {
// 在所有發送請求的操作(open, send)之前執行
console.log('beforeSend', xhr)
},
success: function (res) {
// 隱藏 loading
// 只有請求成功(狀態碼為200)才會執行這個函數
console.log(res)
},
error: function (xhr) {
// 隱藏 loading
// 只有請求不正常(狀態碼不為200)才會執行
console.log('error', xhr)
},
complete: function (xhr) {
// 不管是成功還是失敗都是完成,都會執行這個 complete 函數
console.log('complete', xhr)
}
})

</script>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM