【问题标题】:method post failed to load resource the server responded with a status 404方法 post 加载资源失败,服务器以状态 404 响应
【发布时间】:2021-10-20 20:21:56
【问题描述】:

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

POST http://localhost:63342/insacol/static/api/users 404(未找到)。注册用户.js:20 Uncaught (in promise) SyntaxError: Unexpected token

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()
  }

java代码:

@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>

【问题讨论】:

  • 是你的 spring boot 中定义的路径。从您分享的代码示例中很难分辨。如果您要分享一个可运行的示例会更好。
  • 当您console.log(JSON.stringify(dates) 时会发生什么?格式是否正确?你确定你调用的 URL 是正确的吗? Spring boot 通过哪个端口运行?因为63342 对我来说似乎有点不寻常

标签: javascript java spring spring-boot maven


【解决方案1】:

尝试改变这一点:

@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有关;

【讨论】:

    猜你喜欢
    • 2016-11-01
    • 2018-01-29
    • 2020-07-04
    • 1970-01-01
    • 2019-07-04
    • 2021-11-20
    • 2018-02-26
    相关资源
    最近更新 更多