码迷,mamicode.com
首页 > Web开发 > 详细

原生js写ajax结合promise对象

时间:2020-05-02 12:11:19      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:end   table   eject   xmlhttp   pre   highlight   ons   cell   order   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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));
      }
     }
    }
  })
}

 

原生js写ajax结合promise对象

标签:end   table   eject   xmlhttp   pre   highlight   ons   cell   order   

原文地址:https://www.cnblogs.com/firework-hy/p/12817211.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!