为什么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;
}
首先我们先看代码:
url: urlMap.saveOrUpdateGroupInfo,
type: 'post',
async: false,
dataType: 'json',
data: $("#groupInfo").serialize()
}).done(function(res) {
console.log(res);
if(res.success) {
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;
}
}
Spring对我的返回值进行了映射,但映射结果对应不到视图,所以出现404
解决方法:
1.得把Test01写成Servlet,并映射到某个 URL 上。
2.把Test01改成JSP页面,然后直接请求这个JSP页面。