如何修改axios中headers的content-type
时间: 2024-02-03 22:04:00 浏览: 22
要修改 axios 中 headers 的 content-type,可以使用 axios.defaults.headers.common 属性,将其设置为所需的值。例如,要将 content-type 设置为 application/json,可以使用以下代码:
```
import axios from 'axios';
axios.defaults.headers.common['Content-Type'] = 'application/json';
```
这将在每个 axios 请求中都包含一个名为 Content-Type 的 header,并将其值设置为 application/json。
相关问题
axios请求头content-type
axios请求头content-type是指在使用axios发送请求时,设置请求头中的content-type类型。这个类型通常用于告诉服务器请求体中的数据类型是什么,以便服务器能够正确地解析请求体中的数据。
以下是两个设置axios请求头content-type的例子:
1.将content-type设置为application/x-www-form-urlencoded类型
```javascript
axios.defaults.headers["Content-Type"] = "application/x-www-form-urlencoded";
```
2.将content-type设置为application/json类型
```javascript
axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";
```
需要注意的是,如果不设置content-type类型,axios默认的请求头content-type类型是’application/json;charset=utf-8’,并且发送post请求时默认是直接把 json 放到请求体中提交到后端的。
如何在axios中发送content-type的请求
在axios中发送content-type的请求,需要在请求的配置(config)中添加headers字段,指定Content-Type的值。例如,发送application/json格式的POST请求,代码如下:
```javascript
axios.post('/api', {
data: {
name: 'Alice',
age: 18
}
}, {
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
})
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
```
其中,headers字段指定了Content-Type的值为application/json;charset=UTF-8。需要注意的是,如果发送的是FormData格式的请求,Content-Type应该设置为multipart/form-data。