vue项目知识点(1)

一、qs.parse()、qs.stringify()、JSON.parse()、JSON.stringify()使用方法

参考:https://www.cnblogs.com/LSSSunshine/p/7340573.html

qs使用示例:
文件夹如下:ajax.js 封装ajax请求方法
在这里插入图片描述

import axios from 'axios'

let qs = require('qs')
let root = process.env.API_ROOT
let errorMsg = {
  '400':'系统繁忙,请稍后再试',
  '403':'无该功能权限',
  '410':'重复请求',
  '500':'网络异常,请稍后再试',
}

// axios.defaults.headers.common['token'] = 'bdec066f-4bf7-424e-9237-50a5145ae36a' //lwz 列表
// axios.defaults.headers.common['token'] = '4206b5cc-80fd-40bf-92c2-15b74bce55c5'//

if (localStorage['token'] !== null) {
  axios.defaults.headers.common['token'] = localStorage['token'];
}
axios.defaults.headers.common['platform'] = "g";
axios.defaults.headers.common['cid'] = localStorage['cid'];

let api = {
  post: function(url, param, type = 'init') {
    let headers = {}
    if (type === 'md') {
      headers['ERPSignSecret'] = param.md
      headers['ERPVersion'] = 'V2'
      headers['PlatForm'] = 11
      delete param.md
    }
    return axios
      .post(url, qs.stringify(param), { headers: headers })
      .then(res => {
        return res
      })
  },
  postJSON: function(url, param) {
    let jsonParam = JSON.stringify(param)
    console.log(jsonParam)
    let headers = {
      post: {}
    }
    headers.post['Content-Type'] = 'application/json'
    return axios.post(url, jsonParam, { headers: headers }).then(res => {
      return res
    })
  },
  putJSON: function(url, param) {
    let jsonParam = JSON.stringify(param)
    let headers = {
      put: {}
    }
    headers.put['Content-Type'] = 'application/json'
    return axios.put(url, jsonParam, { headers: headers }).then(res => {
      return res
    })
  },
  get: function(url, param, type = 'init') {
    let headers = {}
    if (type === 'pt') {
      headers['platform'] = 'g'
    }
    return axios.get(url,{ headers: headers ,params:param}).then(res => {
      return res
    })
  },
  delete: function(url, param) {
    return axios
      .delete(url, {
        params: param
      })
      .then(res => {
        return res
      })
  },
  put: function(url, param) {
    return axios.put(url, qs.stringify(param)).then(res => {
      return res
    })
  }
}
export { api,errorMsg }

在vue文件中使用时:

    /**
     * 获取意向区域 展示数据
     */
    getIntentArea() {
      console.log(this.userData)
      let param = {
        cityID: this.userData.cityId || 1
      }
      this.$ajax
        .get('/api/resource/area', param)
        .then(res => {
          // debugger
          res = res.data
          if (res.status === 200) {
            console.log(res)
            res.data.forEach((item, i) => {
              this.$set(this.toPickListArray, i, item)
            })
          }
        })
        .catch(error => {
          this.handleError(error)
        })
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值