ES6系列---【promise中ajax封装函数】

  function sendAjax({type="get",url="",data=null,dataType="json"}) {
      // 该函数的作用就是返回一个改变了状态的(成功或失败)的promise实例对象
    return new Promise((reslove, reject) => {
      $.ajax({
        type,
        url,
        data,
        dataType,
        success: function (response) {
          reslove(response)
        },
        error(err) {
          reject(err)
        }
      });
    })
  }

// 函数的调用:
sendAjax({url:"http://cj.shenzhou888.com.cn/hb_vote/api.php?action=area&id=0"})
.then((data) => {
    console.log( data );
}) .catch((error) => {
    console.log(error);
})
原文地址:https://www.cnblogs.com/chenhaiyun/p/14660502.html