javascript - 方法 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>
解决方案
尝试改变这一点:
@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 有关;
推荐阅读
- dns - 在领事集群设置中使用 systemd-resolved 进行 DNS 转发
- python-tesseract - 将 pytesseract 与旧版引擎一起使用的可维护方式?
- html - 我怎样才能让我的导航栏链接(关于、服务、项目)“停止在屏幕外运行”-让它停止?
- azure - Azure 企业级登陆区域架构的实际实现是什么?
- outlook - 使用 Microsoft Graph API 创建只读日历事件
- snowflake-cloud-data-platform - 如何将 varchar 列与数值进行比较
- r - 如何将 gtsummary 表保存为 tiff
- python - 如何创建 PDF 文件并用 django 打开它们
- git - 突然无法从我的 cpanel 中的存储库中提取更改
- php - Codeigniter 3.1.11 / Jquery (AJAX) 不使用 $.cookie 获取 csrf_cookie_name 的值