前端调用接口404报错

后端:[java] view plain copy RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)public ResponseVo saveOrUpdate(String id, String name, String parentId, String parentName, String operate){ ResponseVo result = new ResponseVo();GroupInfo info = new GroupInfo();Date...
前端调用接口404报错
今天遇到了一个很离奇的场景,使用ajax请求后台结果 后台处理成功了页面还报了404错误。
程序员不说话,默默上代码:
JS:
[javascript] view plain copy
var save = function(){
$.ajax({
url: urlMap.saveOrUpdateGroupInfo,
type: 'post',
async: false,
dataType: 'json',
data: $("#groupInfo").serialize()
}).done(function(res) {
console.log(res);
if(res.success) {

}else{
bootbox.alert(res.msg);

}
});
}
后端:
[java] view plain copy
@RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
public ResponseVo saveOrUpdate(String id, String name, String parentId, String parentName, String operate){
ResponseVo result = new ResponseVo();
GroupInfo info = new GroupInfo();
Date now =new Date();
info.setUpdateTime(now);
try{
if(operate.equals("add")){
info.setParentId(Integer.parseInt(parentId));
info.setName(name);
info.setCreateTime(now);
groupInfoService.addGroup(info);
}else if (operate.equals("edit")) {
info.setId(Integer.parseInt(id));
info.setName(name);
info.setParentId(Integer.parseInt(parentId));
groupInfoService.updateGroup(info);
}else if (operate.equals("delete")) {
groupInfoService.deleteGroup(Integer.parseInt(id));
}
result.setSuccess(true);
}catch (Exception e){
log.error("operate group error."+ JsonUtil.toString(info), e);
result.setSuccess(false);
result.setMsg(e.getMessage());
}
return result;
}
}
挺奇怪吧?
经分析是请求没有返回状态码,这是因为我用的是SpringMVC框架,前后端使用JSON传递数据,因为返回的是对象,而忘记了添加
@ResponseBody
注解,所以 Spring对我的返回值进行了映射,但是映射结果又对应不到视图,所以返回了404
正常后台代码:
[java] view plain copy
@RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
@ResponseBody
public ResponseVo saveOrUpdate(String id, String name, String parentId, String parentName, String operate){
ResponseVo result = new ResponseVo();
GroupInfo info = new GroupInfo();
Date now =new Date();
info.setUpdateTime(now);
try{
if(operate.equals("add")){
info.setParentId(Integer.parseInt(parentId));
info.setName(name);
info.setCreateTime(now);
groupInfoService.addGroup(info);
}else if (operate.equals("edit")) {
info.setId(Integer.parseInt(id));
info.setName(name);
info.setParentId(Integer.parseInt(parentId));
groupInfoService.updateGroup(info);
}else if (operate.equals("delete")) {
groupInfoService.deleteGroup(Integer.parseInt(id));
}
result.setSuccess(true);
}catch (Exception e){
log.error("operate group error."+ JsonUtil.toString(info), e);
result.setSuccess(false);
result.setMsg(e.getMessage());
}
return result;
}2018-02-28
mengvlog 阅读 8 次 更新于 2025-07-21 01:14:34 我来答关注问题0
  •  起航知识小百科 404是前端问题还是后端问题

    404错误的出现既可能是由于后端服务器返回的错误状态码,也可能是由于前端代码的错误导致的。通常情况下,404错误是由后端服务器返回的。当客户端发出请求时,服务器会搜索相应的资源,如果没有找到,则会返回一个404状态码。客户端会解析这个状态码并显示一个“页面未找到”的错误提示。在一些情况下,404...

  •  深空见闻 新来的前端小姐姐问:Vue路由history模式刷新页面出现404问题

    在Vue路由的history模式下刷新页面出现404问题,主要是因为后端没有进行相应配置。以下是解决方案:后端配置通用资源覆盖机制:目的:确保当请求的URL未能匹配到任何静态资源时,返回同一个index.html页面。实现方式:Apache服务器:使用mod_rewrite或FallbackResource配置。Nginx服务器:通过配置实现资源覆盖。Node...

  •  湖北倍领科技 前端请求404是因为什么

    前端请求出现404错误通常是由于文件路径配置不当导致的。当用户通过直接地址访问前端路由时,浏览器会首先向服务器发起请求。如果服务器上并未针对该路由进行相应的处理,即服务器未能找到与访问地址相对应的文件路径,它将返回404错误。在这种情况下,如果后端服务器将所有未匹配到的路由都指向前端处理,前端...

  •  深空见闻 若依配置nginx只有导入接口404其他正常

    若依配置nginx后只有导入接口404其他正常,可能是因为nginx的server_name设置不正确或者location配置有误。一、server_name设置问题 原因:若server_name使用了localhost,而前端请求后端接口时是通过服务器的公网IP进行的,nginx在收到请求后,会检查请求头中的Host字段与server_name配置进行匹配,如果不匹配则...

  •  校企律说法 vue前端系统接口404异常

    第一,检查 vue 客户端,构造 HTTP 请求 URL 的逻辑是否正确。第二,登录 vue 应用连接的后台服务器,在文件系统里,检查前端请求的页面,是否真的存在对应的资源文件。vue前端系统接口404异常 404是找不到,没调通前后台吧。 可能是url不对,可能是请求头不对。检查url先吧 用postman 试试先,再...

檬味博客在线解答立即免费咨询

报错相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部