在axios中二次封装可以选择请求头Content-Type类型
时间: 2024-06-09 15:10:04 浏览: 167
是的,你可以在axios的二次封装中设置请求头的Content-Type类型。一般来说,如果你发送的是表单数据,你可以将Content-Type设置为`application/x-www-form-urlencoded`。如果你发送的是JSON数据,你可以将Content-Type设置为`application/json`。
以下是一个设置Content-Type为`application/json`的示例:
```javascript
import axios from 'axios';
const instance = axios.create({
baseURL: 'https://api.example.com',
headers: {
'Content-Type': 'application/json'
}
});
export default instance;
```
这里我们创建了一个新的axios实例,并设置了请求头的Content-Type为`application/json`。这样在发送POST请求时,axios会自动将data转换为JSON字符串,并设置请求头的Content-Type为`application/json`。
阅读全文
相关推荐















