原生js写ajax结合promise对象
2021-03-13 20:34
标签:end table eject xmlhttp pre highlight ons cell order 原生js写ajax结合promise对象 标签:end table eject xmlhttp pre highlight ons cell order 原文地址:https://www.cnblogs.com/firework-hy/p/12817211.html
const
ajaxPromise = param => {
return
new
Promise((resovle, reject) => {
var
xhr =
new
XMLHttpRequest();
xhr.open(param.type ||
"get"
, param.url,
true
);
xhr.send(param.data ||
null
);
xhr.onreadystatechange = () => {
if
(xhr.readyState === 4){
if
(xhr.status === 200){
resovle(JSON.parse(xhr.responseText));
}
else
{
reject(JSON.parse(xhr.responseText));
}
}
}
})
}