axios发送post请求时,发现是请求头content-type不对,是application/json,应该是application/x-www-form-urlencoded。
解决方法有以下三种:
1、设置axios的默认请求头
//设置全局的 axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; var instance = axios.create({}) // 这样创建出来的 只需要: instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
2、使用URLSearchParams来构建参数
var params = new URLSearchParams(); params.append("userId", _this.userId); params.append("busKey", _this.busKey); axios.post("/api/submit", paramsOfJson ).then(function (response) { console.log(response); }).catch(function (error) { console.log(error); })
3、后台使用@requestBody接收
@PostMapping(value = "/submit") public String testLogin(@RequestBody Map dataMap)