mpvue入坑之旅(三)网络请求

mpvue入坑之旅(三)网络请求

1 封装请求方法

在小程序环境下,只能使用小程序官方提供的 wx.request 方法发送外部请求,但这个方法有很多冗余,所以在开发前,先封装一下。
1)在utils目录下,新建文件 network.js

//请求地址
let apiUrl= 'http://www.abc.com/api/'

function request(url, method, data, header = {}) {
  wx.showLoading({
    title: '加载中'
  })
  return new Promise((resolve, reject) => {
    wx.request({
      url: apiUrl + url,
      method: method,
      data: data,
      headers: {
        'content-type': 'application/json' // 默认转为json格式
      },
      success: function(res) {
        wx.hideLoading()
        resolve(res.data)
      },
      fail: function(res) {
        wx.hideLoading()
        reject(res.data)
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  })
}

function get(url, data) {
  return request(url, 'GET', data)
}

function post(url, data) {
  return request(url, 'POST', data)
}

export default {
  get,
  post
}

2)根目录下main.js引入network.js

import Vue from 'vue'
import App from './App'
import network from './utils/network'
Vue.prototype.$http = network

Vue.config.productionTip = false
App.mpType = 'app'

const app = new Vue(App)
app.$mount()

3)页面中直接使用

this.$http.post({
  '/login', {'token': 'token'}
}).then(res =>{
  console.log(res.data)
});

2 实例

1)springboot搭建后台,可以参考 https://blog.csdn.net/qq_23470315/article/details/105288377

2)demo中index有绑定点击方法,稍作修改测试网络请求
在这里插入图片描述

bindViewTap () {
  this.$http.post({
    '/wx/login', { }
  }).then(res => {
    console.log(res)
  })
},

3)执行 cnpm run build,编译
4)触发点击事件,查看日志(请求成功了,这里报错是接口的校验问题,下节了解微信登录)
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
mpvue 中使用 axios 处理 HTTP 请求,你需要先安装 axios,并在代码中引入和配置 axios。 以下是一个示例代码,展示了在 mpvue 中使用 axios 处理 HTTP 请求的方法: 1. 安装 axios: 在项目根目录下打开终端,执行以下命令安装 axios: ``` npm install axios --save ``` 2. 创建一个封装 axios 的文件(例如 `api.js`): 在项目的 `src` 目录下新建一个 `api.js` 文件,并编写以下代码: ```javascript import axios from 'axios'; const service = axios.create({ baseURL: 'http://api.example.com', // 设置请求的基础 URL timeout: 5000 // 设置请求超时时间 }); export default service; ``` 3. 在需要发送请求的地方使用 axios: 在你需要发送 HTTP 请求的组件中,引入刚才创建的 `api.js` 文件,并使用 axios 发送请求。 ```vue <template> <div> <button @click="getData">获取数据</button> <p>{{ response }}</p> </div> </template> <script> import api from '@/api.js'; export default { data() { return { response: '' }; }, methods: { getData() { api.get('/data') // 发送 GET 请求 .then(response => { this.response = response.data; }) .catch(error => { console.error(error); }); } } } </script> ``` 在上述代码中,我们首先在 `api.js` 文件中配置了 axios 的基本设置,并导出了一个实例化的 axios 对象。然后,在组件中引入 `api.js` 文件,并在 `getData` 方法中使用 axios 发送 GET 请求请求成功后,将响应数据赋值给 `response` 变量。 当点击按钮时,`getData` 方法会被调用,发送请求并将响应数据显示在页面上。 这是一个简单的示例,你可以根据具体的项目需求和接口文档来配置和使用 axios。 希望以上信息对你有所帮助!如果你还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值