当参数为对象时,参数在request payload里,content-type为application/json;charset=UTF-8,这样springmvc是不能通过
request.getParameterMap();获取的解决办法一,参数通过
URLSearchParams设置
let param = new URLSearchParams();
param.append("username", "admin");
param.append("password", "admin");
方法二,修改axios源码-axios--lib---defaults.js
defaults.js文件里transformRequest方法中
if (utils.isObject(data)) {
//setContentTypeIfUnset(headers, ‘application/json;charset=utf-8‘);
//return JSON.stringify(data);
setContentTypeIfUnset(headers, ‘application/x-www-form-urlencoded;charset=utf-8‘);
let keys2 = Object.keys(data);
/* 这里就是把json变成url形式,并进行encode */
//console.log(‘key====‘, keys2)
return encodeURI(keys2.map(name => `${name}=${data[name]}`).join(‘&‘));
}
这样就可以了
原文地址:https://www.cnblogs.com/huadiwxg/p/8184341.html
时间: 2024-11-09 16:28:42