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("username", _this.username);params.append("password", _this.password);axios.post("/service/login", paramsOfJson ).then(function (response) { console.log(response); }).catch(function (error) { console.log(error); })3、后台使用@requestBody接收
@PostMapping(value = "/login")public String testLogin(@RequestBody Map dataMap)上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
vue 过滤器filter实例详解
vue页面切换到滚动页面显示顶部的实例_vue.js
聊聊JS动画库 Velocity.js的使用
可以把后台的代码发出来看看springmvc获取参数1.通过@PathVariable获取路径中的参数@RequestMapping(value="/addUser/{username}/{password}",method=RequestMethod.GET)public String addUser(@PathVariable String username,@PathVariable String password) {System.out.println("username is:"+username);System.out.println("password is:"+password);return "demo/index";}2.用注解@RequestParam绑定请求参数到方法入参当请求参数username不存在时会有异常发生,可以通过设置属性required=false解决,例如: @RequestParam(value="username", required=false)