原生ajax
ajax依賴XMLHttpRequest物件
? var xhr = new XMLHttpRequest();
? xhr 有兩個方法
? ①open(‘GET’,url,ture) 請求初始化 引數說明: 第一個引數:請求方式(比如get post) 第二個引數: url地址 第三個引數: 默認true
? ②send() 發送請求
? xhr 有兩個狀態
? ①readyState
? 0 請求未初始化 還未呼叫open方法
? 1 呼叫open方法, 但還沒有呼叫send方法
? 2 send方法被呼叫了 請求正在發送…
? 3 請求正在處理
? 4 請求成功 有回傳值
? ②status
? 200 請求成功
? 3xx 重定向 資源未找到,又指向另外一個地址
? 404 資源未找到 客戶端錯誤
? 500 服務端錯誤
? xhr.responseText
? 后端回傳的資料
<script>
var xhr = new XMLHttpRequest(); // ajax依賴 XMLHttpRequest物件
xhr.onreadystatechange = function() {
console.log(xhr.readyState);
console.log(xhr.status);
if (xhr.readyState === 4 && xhr.status === 200) {
console.log(xhr.responseText);
}
}
xhr.open('GET', 'http:10.50.5.75:3000/userList', true);
xhr.send();
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/246185.html
標籤:其他
上一篇:超炫100套?vue/react+echarts? 大屏可視化資料平臺實戰專案分享 (附原始碼)
下一篇:前端課程體系
