vueコンポーネントのパッケージのaxiosベースのajax要求方法

1594 ワード

以下の通りです

import Vue from 'vue'
 
let service = {
 url: 'http://host.xxxxx.com/xxx.php'
}
 
service.ajaxReuqest = (url, options, type, fileFlag) => {
 for (const i in options) {
 if (!options[i] && options[i] !== 0 && (options[i].length && options[i].length > 0)) {
  delete options[i]
 }
 }
 let promise = new Promise((resolve, reject) => {
 if (fileFlag) {
  Vue.axios.post(url, options, {
  headers: {
   'Content-Type': 'multipart/form-data'
  }
  }).then((res) => {
  resolve(res)
  })
 } else if (type === 'GET') {
  Vue.axios.get(url, { params: options }).then((res) => {
  resolve(res.data.resultObj)
  }).then((err) => {
  reject(err)
  })
 } else {
  Vue.axios.post(url, options).then((res) => {
  resolve(res)
  }).then((err) => {
  reject(err)
  })
 }
 })
 return promise
}
POST GET要求及び画像アップロードをサポートします。axiosに基づいて、vueに適用します。
非同期で省リストを取得する例:

//       
service.getProvinceList = (options) => {
 return service.ajaxRequest(service.url + '/basic/getProvinceList', options, 'POST')
}

getProvinceList () {
 service.getProvinceList({}).then((res) => {
  this.provinceList = res.data.resultObj.data
 })
}
以上のこのvueコンポーネントのパッケージはaxiosのajax要求方法に基づいています。小编で皆さんに提供した内容は全部分かります。