传统的ajax写法:
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.responseType = 'json';
xhr.onload = function(){
console.log(xhr.response);
};
xhr.onerror = function(){
console.log("error")
}
xhr.send();
fetch写法:
fetch(url).then(function(response){
return response.json()
}).then(function(data){
console.log(data)
}).catch(function(err){
console.log(err)
})
利用async、await
try{
let response = fetch(url);
let data = await response.json();
return data;
} catch (err){
console.log(err)
}
fetch api 点击这里
兼容ajax 和 fetch的实现:判断浏览器是否支持 fetch 不支持则利用 promise 返回
let baseUrl = 'localhost:8088&