fetch

与XMLHttpRequest(XHR)类似,fetch方法也会允许发送ajax请求,区别在于fetch 的API使用promise,更加简单易用。

promise是一个对象,有三种状态 pending,resolve,reject

AJAX的实现方式

function reqListenser(){

var data = JSON.parse(this.responseText)

console.log(data)

}

function reqError(error){

console.log(err)

}

var xhr = new XMLHttpRequest();

xhr.onload = reqListener;

xhr.onerror = reqError;

xhr.open('get',url,true);

xhr.send();

fetch(url).then(function(res){

if(res!=200){

console.log(error)

return;

}

res.json().then(function(data){

console.log(data)

}).catch(function(error){

console.log(error)

})

})



猜你喜欢

转载自blog.csdn.net/qq_40639990/article/details/80229934