首页 > 解决方案 > 方法 post 加载资源失败,服务器以状态 404 响应

问题描述

我是 Springboot 的新手,所以我收到了这个错误

发布 http://localhost:63342/insacol/static/api/users 404(未找到)。registerUser.js:20 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

js代码:

// Call the dataTables jQuery plugin
$(document).ready(function() {

});


  async function registerUser(){
      let dates ={};
      dates.name = document.getElementById('txtName').value;
      dates.lastname = document.getElementById('txtLastName').value;
      dates.email = document.getElementById('txtEmail').value;
      dates.password = document.getElementById('txtPassword').value;
      let repeatPassword = document.getElementById('txtRepeatPassword').value;

      if (repeatPassword != dates.password){
          alert('La contraseña es diferente');
          return;
      }
    console.log(dates);
    const request = await fetch('api/users', { method: 'POST', headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      }
    });
    body: JSON.stringify(dates)
    const users = await request.json()
  }

爪哇代码:

@RequestMapping(value = "api/users", method = RequestMethod.POST)
    public void insertUser(@RequestBody User user){
        userDao.insertar(user);
    }

html代码:

<a onclick="registerUser()" href="#" class="btn btn-primary btn-user btn-block">Register Usuario</a>

标签: javascriptjavaspringspring-bootmaven

解决方案


尝试改变这一点:

@RequestMapping(value = "api/users", method = RequestMethod.POST)
public void insertUser(@RequestBody User user){
    userDao.insertar(user);
}

有了这个:

@RequestMapping(value = "insacol/static/api/users", method = RequestMethod.POST)
public void insertUser(@RequestBody User user){
    userDao.insertar(user);
}

问题应该与您调用的 URL 有关;


推荐阅读