axios请求415错误Uncaught (in promise) Error: Request failed with status code 415

ゝ一世哀愁。 2024-04-18 23:05 150阅读 0赞

Uncaught (in promise) Error: Request failed with status code 415

错误如下图
415错误
前端代码(vue):

  1. var data = {
  2. username: this.loginForm.username,
  3. password: this.loginForm.password
  4. }
  5. this.$axios.post(this.GLOBAL.host + '/login', this.$qs.stringify(data)
  6. ).then(res => {
  7. // Determine the login status based on the returned results
  8. console.log(res)
  9. })

后台代码(springboot):

  1. @Controller
  2. public class LoginController {
  3. @Autowired
  4. private LoginService loginService;
  5. @RequestMapping(value = "/login",method = RequestMethod.POST)
  6. @ResponseBody
  7. public Object login(@RequestBody UserDTO userDTO){
  8. return loginService.login(userDTO.getUsername(),userDTO.getPassword());
  9. }
  10. }

错误原因:

当我们使用application / x-www-form-urlencoded时,Spring并不将其理解为RequestBody。因此,如果我们想要使用它,我们必须删除@RequestBody注释。

然后尝试以下方法:
修改LoginController代码:

  1. @Controller
  2. public class LoginController {
  3. @Autowired
  4. private LoginService loginService;
  5. @RequestMapping(value = "/login",method = RequestMethod.POST,
  6. consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
  7. produces = {MediaType.APPLICATION_ATOM_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
  8. @ResponseBody
  9. public Object login(UserDTO userDTO){
  10. return loginService.login(userDTO.getUsername(),userDTO.getPassword());
  11. }
  12. }

重新试一下~~访问成功,问题解决啦
成功

发表评论

表情:
评论列表 (有 0 条评论,150人围观)

还没有评论,来说两句吧...

相关阅读