原生js写ajax结合promise对象

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));
       }
      }
     }
   })
}

 

猜你喜欢

转载自www.cnblogs.com/firework-hy/p/12817211.html
今日推荐