一、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)
})
},