mpvue使用wx.request封裝請求資料
阿新 • • 發佈:2018-11-29
1.建立src下建立utils/wx-request.js
const host = 'https://rmall.ukelink.net' function request (url, method, data, header = {}) { wx.showLoading({ title: '載入中' // 資料請求前loading }) return new Promise((resolve, reject) => { wx.request({ url: host + url, // 僅為示例,並非真實的介面地址 method: method, data: data, headers: {'content-type': 'application/json' // 預設值 }, success: function (res) { wx.hideLoading() resolve(res.data) }, fail: function (res) { wx.hideLoading() // reject(false) }, complete: function () { wx.hideLoading() } }) }) } functionget (obj) { return request(obj.url, 'GET', obj.data) } function post (obj) { return request(obj.url, 'POST', obj.data) } export default { request, get, post, host }
2.main.js中引入到原型
import WXrequest from './utils/wx-request' Vue.prototype.$httpWX = WXrequest
3.使用
this.$httpWX.post({ url:'/mms/country/queryValidZoneListForMallHome', data: { 'categoryType': '[email protected]', 'streamNo': 'web_bss153570682909641893', 'reqSource': 'MALL_H5', 'appid': 'string', 'timestamp': 1535706829096, 'sign': 'string' } }).then(res => { console.log(res) })