axios请求头content-type
时间: 2023-11-20 14:58:46 浏览: 95
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
我可以给你一个示例:const axios = require('axios');
axios({
method: 'post',
url: '/user/12345',
headers: {
'Content-Type': 'application/json'
}
});
axios设置请求头content-type
可以使用axios.defaults.headers来设置请求头中的content-type类型,具体方法如下:
```javascript
// 设置content-type为application/x-www-form-urlencoded
axios.defaults.headers["Content-Type"] = "application/x-www-form-urlencoded";
```
如果需要发送post请求并将数据以json格式提交到后端,可以使用以下代码:
```javascript
// 设置content-type为application/json
axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";
// 发送post请求并提交json数据
axios.post(url, {
data: JSON.stringify(data)
}).then(response => {
console.log(response);
}).catch(error => {
console.log(error);
});
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)