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.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3ab17-345ff.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3ab17-345ff.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?