前言:均以post方法为例
这是接手别人开发了一半的项目,其中ajax的各类请求都已经封装好,header中均为:‘Content-Type’: ‘application/x-www-form-urlencoded’。
post (url, params) {
return FEBS_REQUEST.post(url, params, {
transformRequest: [(params) => {
let result = ''
Object.keys(params).forEach((key) => {
if (!Object.is(params[key], undefined) && !Object.is(params[key], null)) {
result += encodeURIComponent(key) + '=' + encodeURIComponent(params[key]) + '&'
}
})
return result
}],
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
},
但新的功能后台要求为application/json,因此对这些请求都进行了重新封装,如下:
jsonPost (url, data) {
return FEBS_REQUEST.post(url, data, {
headers: {
'Content-Type': 'application/json'
}
})
},