前端调用接口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 阅读 477 次 更新于 2025-09-12 06:18:28 我来答关注问题0
  •  深空见闻 前端请求接口超时老是报404

    前端请求接口超时老是报404的原因可能有多种,主要包括URL地址错误、文件或资源不存在、后端服务未重启以及路由配置问题。1. URL地址错误:这是最常见的原因之一。前端或后端开发者可能在编写URL时出现了拼写错误,如字母大小写错误、路径错误,或者在URL中不小心加入了不必要的字符,如逗号等。这些错误都...

  •  阿暄生活 ruoyi前端页面发送请求返回404

    原因:前端请求的baseURL与后端服务的URL不一致,或者前端请求的路径与后端Controller中定义的路径不匹配,都会导致404错误。解决方案:仔细检查前端请求的URL和后端Controller的路径,确保它们完全一致。这包括检查端口号、路径名以及任何查询参数或路径变量。3. 后端服务未启动或重启 原因:在修改后端代码后,...

  •  深空见闻 post请求,通过controller路径接收为什么会报404

    如果使用Controller作为bean注册容器而不使用RestController,则需要在接口方法上添加@ResponseBody注解,以确保返回的数据以json格式返回。否则,即使后台看到请求成功,前端也可能收到404错误。综上所述,解决这类问题需要从多个方面进行检查和调整,以确保URL与注解一致、Controller正确注册、部署配置无误以及Contr...

  •  翡希信息咨询 前端请求404是因为什么

    前端请求404错误主要是由于文件路径配置不当或路由配置不全导致的。具体原因及解决方法如下:文件路径配置不当:当用户通过直接地址访问前端路由时,浏览器会向服务器发起请求。如果服务器上未针对该路由进行相应处理,即服务器未能找到与访问地址相对应的文件路径,就会返回404错误。后端路由配置问题:后端Contr...

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

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

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

报错相关话题

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