uniapp+vue+axios真机无法发送请求

崭新出炉的项目中没有使用官方提供的uni.request,使用了axios,本地调试没问题,真机运行时无法发送请求,提示adapter is not a function

解决方案:配置axios适配器(axios-adapter-uniapp)

命令npm install axios-adapter-uniapp

使用

import axios from 'axios'
import axiosAdapterUniapp from 'axios-adapter-uniapp'

const instance = axios.create({
  baseURL: 'URL.com',
  adapter: axiosAdapterUniapp,
})

// request拦截器
instance.interceptors.request.use()

// respone拦截器
instance.interceptors.request.use()

export default instance

GET

import axios from '@/common/js/axios/index.js'

axios({
  method: 'get',
  url: '/user',
  data: {
    id: 1,
  },
})

axios({
  method: 'get',
  url: '/user',
  params: {
    id: 1,
  },
})

// 两者结果相同 =>
// URL.com/user?id=1

POST

import axios from '@/common/js/axios/index.js'

axios({
  method: 'post',
  url: '/user',
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone',
  },
})

剩下的可自行浏览官方文档

https://www.npmjs.com/package/axios-adapter-uniapp

你可能感兴趣的:(vue.js,uni-app,前端)