一、
问题:react用fetch传递json数据给后台,但一直报400,后台提示Required String parameter ‘username’ is not present
解决:百度搜到一个老哥的博客https://blog.csdn.net/Stitch__/article/details/89501492写的很好
问题出在fetch中的请求头设置,前端默认的转递方式是content-type = application/x-www-form-urlencoded,传递的是json 数据对象 ,需要后端使用@RequestParam() 接收数据;而若是content-type = application/json,传递的是 json 数据字符串,后端接收数据使用@RequestBody Map<String,Object>map 然后在map 中获取相应的数据!
如:前端fetch请求格式
export function login(params){
console.log(params);
return(myfetch('/login', 'POST',JSON.stringify(params)))
.then((res) => {return res.json(); })
.then((res) => {
console.log(res);
const accounts = {
isFetching: false,
result:res
}