MpVue踩坑之flyio post序列化传参数

先看下完整的封装flyio


import wx from './wx'

import qs from 'qs'

var Fly = require('flyio/dist/npm/wx')

const request = new Fly()

request.config.timeout = 10 * 1000

request.interceptors.request.use((request) => {

  if (request.method === 'POST' || request.method === 'post') {
    request.body = qs.stringify(request.body)
  }

  wx.showLoading({ title: '拼命加载中...' })

  return request

})

request.interceptors.response.use(

  (response, promise) => {

    wx.hideLoading()

    return promise.resolve(response.data)

  },

  (err, promise) => {

    wx.hideLoading()

    wx.showToast({

      title: err.message,

      icon: 'none'

    })

    return promise.resolve()

  }

)

export default request

首先是引入import qs from 'qs'

设置拦截器对参数进行序列化


request.interceptors.request.use((request) => {

  request.body = qs.stringify(request.body)

  wx.showLoading({ title: '拼命加载中...' })

  return request

})

你可能感兴趣的:(MpVue踩坑之flyio post序列化传参数)