为什么我的AJAX请求处理成功了,还报404错误?

今天遇到了一个很离奇的场景,使用ajax请求后台结果 后台处理成功了页面还报了404错误。




程序员不说话,默默上代码:

JS:

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);
              
            }
        });
}
后端:

  @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

正常后台代码:

@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;
    }

 


评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值