如何在axios中发送content-type的请求
时间: 2023-09-25 20:13:02 浏览: 76
在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。