解决方案 »

  1.   

    错误的请求,,你先在浏览器看看url:'/demo/cart/buy',  这个是否可以访问,然后再排查ajax语法是否有错误
      

  2.   

    别用success这个字作为键,改为别的试试。
      

  3.   


    直接访问是这个结果,应该不管用的:the server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method. 
    服务器拒绝服务,原因是请求格式不被支持 
      

  4.   

    我没用过spring mvc  但是你这里 method = {RequestMethod.POST,RequestMethod.GET}) 又是指定post 又是get  是个什么意思??
      

  5.   

    这个既可以接受post,又可以接受get的请求的,当时有问题,我就换了方式来回测试,都写了,也省的麻烦修改。。这个是没有问题的。。
      

  6.   

    @RequestBody Integer id 改成string id 试试吧
      

  7.   

    不太熟悉,凭直觉觉得:方法的签名不对吧?
    它允许带这样3个参数么?是不是应该自己从request里面取出id啊?
      

  8.   

    1、你的项目名称是不是叫做 demo?
    2、你的action类上有没有做 RequestMapping?
    如果你的项目名称叫做demo ,action 没有 RequestMapping,
    此时 你的 addUser 方法的注解要改成 RequestMapping(value = "/cart/buy"....如果你的项目名称不叫 demo:
    1、那么你的ajax请求的url那就有问题,因为根本没访问到后台,所以404;
    2、那么你的addUser方法要改成  RequestMapping(value = "/demo/cart/buy".......
      

  9.   

    action类上有requestMapping("cart")了,应该不是请求路径的问题,我的错误时400不是404.。。
      

  10.   

    哦,没注意,不好意思,你的spring 过滤的是什么后缀?
      

  11.   

    url:'/demo/cart/buy' 需要加 http://localhost:8088/demo/cart/buy' 
      

  12.   

    400这个不是服务器的错误么?应该是你启错服务器了。碰到过用myeclipse自带服务器部署项目,请求的时候没有页面出来,报400错误
      

  13.   

    但是我其他的页面跳转没有问题,就是这个ajax有问题。。
      

  14.   

    解决了,红色是和以前不一样的(先谢谢以上各位朋友解答。。): $.ajax({
    type:'post',
    url:'/demo/cart/buy',
    data:{ "id": id },//没有contentType: "application/json; charset=utf-8",
    dataType:'json',
    success:function(data){
    if(data.success1){
    $("#cartinfo_"+id).html("购买成功");
    }else{
    $("#cartinfo_"+id).html("*请到购物车里修改数量");
    }
    },
    error:function(){

    }
    });后台: @RequestMapping(value = "/buy", method = {RequestMethod.POST,RequestMethod.GET})  
     @ResponseBody  
     public Map<String, String> shoppingCart(Integer id, HttpServletRequest request,HttpServletResponse response) {  
        Map<String, String> map = new HashMap<String, String>(1); 
        try {
    addToShoppingCart(id, request, response);
    } catch (Exception e) {
     map.put("success1", "false");  
    }
        map.put("success1", "true");
        return map;  
      }  
      

  15.   

    对的,去掉@RequestBody 这个请求进去这个方法了。。还要去掉contentType: "application/json; charset=utf-8",这个