亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于Vue的ajax公共方法(詳解)

 更新時(shí)間:2018年01月20日 16:47:13   作者:GDUT_YIFEI  
下面小編就為大家分享一篇基于Vue的ajax公共方法(詳解),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

為了減少代碼的冗余,決定抽離出請(qǐng)求ajax的公共方法,供同事們使用。

我使用了ES6語法,編寫了這個(gè)方法。

/**
  * @param type 請(qǐng)求類型,分為POST/GET
  * @param url 請(qǐng)求url
  * @param contentType
  * @param headers
  * @param data
  * @returns {Promise<any>}
  */
 ajaxData: function (type, url, contentType, headers, data) {
  return new Promise(function(resolve) {
   $.ajax({
    type: type,
    url: url,
    data: data,
    timeout: 30000, //超時(shí)時(shí)間:10秒
    headers: headers,
    success: function(data) {
     resolve(data);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
     resolve(XMLHttpRequest);
    }
   });
  });
 }

通過回調(diào)函數(shù)的方式返回請(qǐng)求結(jié)果。

測(cè)試代碼如下:

getAjaxDataMethod: function () {
    const url = "";
    const type = "POST";
    const contentType = "application/json";
    const headers = {};
    const data = {};
    Api.ajaxData(type, url, contentType, headers, data).then(function (res) {
     console.log(res);
    }).catch(function (err) {
     console.log(err);
    })
   }

測(cè)試通過!

以上這篇基于Vue的ajax公共方法(詳解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論