首页 > 微信 > 详细

mpvue 小程序 使用wx.request请求数据

时间:2019-11-15 17:14:30      阅读:221      评论:0      收藏:0      [点我收藏+]

1.创建src下创建utils/wx-request.js

const host = ‘http://10.0.0.6:8081‘
 
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()
      }
    })
  })
}
 
function get (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 Vue from ‘vue‘
import App from ‘./App‘
import WXrequest from ‘./utils/wx-request‘

Vue.prototype.$httpWX = WXrequest

3.使用

   this.$httpWX.post({
      url: ‘/data.json‘,
      data: {
        
      }
    }).then(res => {
      console.log(res)
    })

 

mpvue 小程序 使用wx.request请求数据

原文:https://www.cnblogs.com/zyfenblog/p/11867401.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!