fetch

Similar to XMLHttpRequest (XHR), the fetch method also allows ajax requests to be sent. The difference is that the fetch API uses promises, which are easier to use.

Promise is an object with three states pending, resolve, reject

Implementation of 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)

})

})



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326054770&siteId=291194637