Objects sent by ajax can only be received by string, not by json.

the front end writes an ajax

 <script>
    test.onclick=function () {
        var user = {
            "productId":1000,
            "productQuantity":1
        };

        alert(11);
        $.ajax({
            type: "POST",
            dataType:"json",
            async:"true",
            contentType: "application/json",
            url: "/campus_ordering_system/master/buycar/list",
            data: JSON.stringify(user) ,
            success:function(data){
                //TODO
                alert(1);
            },
            error:function () {
                alert(user.productId);
                alert(2);
            }
        })


    }

</script>

here, you can get the user object with string, but it comes out as a string of {"productId": 1000, "productQuantity": 1}. If you change it to JSONObject, the received value is null

.
  @RequestMapping("/list")
    @ResponseBody
    public ModelAndView  list(HttpServletRequest request,@RequestBody String params){
        HttpSession session=request.getSession();
        System.out.println(":"+params);
        System.out.println(":"+request.getParameter("productId"));
        ProductInfo productInfo=productInfoService.findByOneProductInfo("1000");
        session.setAttribute("data",productInfo);

        return new ModelAndView("buycar/buycar");
    }
Jul.25,2021

AJAX should not be transmitted in JSON mode, otherwise, please parse JSON


to create the same object as the front end. You can directly map


suggest that you set up a User class in the background, including productId and productQuantity attributes, and use @ RequestBody User user form to map the value from the front end to user.

Menu