为什么我的AJAX请求处理成功了,还报404错误
2个回答
展开全部
今天遇到了一个很离奇的场景,使用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;
}
程序员不说话,默默上代码:
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;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码截图贴上来
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询