要在axios的post请求头中添加"Content-Type",可以通过设置axios的默认请求头来实现。示例代码如下:
import axios from 'axios';
axios.defaults.headers.post['Content-Type'] = 'application/json';
// 发送post请求
axios.post(url, data)
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
在上述代码中,我们通过axios.defaults.headers.post来设置默认的post请求头,将"Content-Type"设置为"application/json"。这样在发送post请求时,就会自动添加该请求头。
如果你想在某个特定的post请求中添加不同的请求头,可以通过在请求的config对象中设置headers属性来实现。示例代码如下:
import axios from 'axios';
// 发送post请求
axios.post(url, data, {
headers: {
'Content-Type': 'application/json'
}
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
在上述代码中,我们通过在请求的config对象中设置headers属性,将"Content-Type"设置为"application/json",这样在这个特定的post请求中就会添加该请求头。
下一篇:Axios的POST请求未被保存