vue 数据请求content-type 全局设置和部分覆盖设置
1.全局设置:
在request.js中设置
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
2.部分接口不是json 格式传输时,设置单独的 Content-Type
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
// 登录方法
export function login(username, password, code, uuid) {
const data = {
username:username,
password:password
}
return request({
url: '/user/login',
method: 'post',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: data,
transformRequest: [function (data) {
// 数据默认会以json格式传递,需要转成key-value
let ret = '';
for (let i in data) {
ret += encodeURIComponent(i) + '=' + encodeURIComponent(data[i]) + '&'
}
return ret.slice(0, -1);
}],
})
}
注意: Content-Type 大写
本文介绍了在Vue应用中如何全局和部分接口设置Content-Type,以适应不同的数据传输格式。在request.js中可以设置axios的默认Content-Type为'application/json;charset=utf-8',而在需要使用'application/x-www-form-urlencoded'格式的接口中,可以通过覆盖headers并使用transformRequest转换数据格式。详细步骤和代码示例帮助开发者理解并实现不同数据格式的请求。

1560

被折叠的 条评论
为什么被折叠?



