ES6系列---【promise中ajax封裝函數】

   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);
})
相關文章
相關標籤/搜索