axios put请求错误Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

前端使用的封装的 axios请求,后端java
出现上面的问题是因为前后端数据格式不统一。
json 分为两种类型;
(1) json 对象类型,即前端定义的Content type 为 application/x-www-form-urlencoded等
(2) json字符串类型,即前端定义的Content type 为 application/json

打印一下put

console.log(instance.defaults.headers.put)

很显然,put默认的Content type 是第一种
在这里插入图片描述
所以,解决这个问题,设置默认put的content-type 为application/json; charset=UTF-8,与后端保持一致即可。

import axios from 'axios';
const instance = axios.create({});
instance.defaults.headers.put['Content-Type'] = 'application/json; charset=UTF-8';
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章