Spring @RequestParam arguments not being passed in POST method

前端 未结 1 1233
生来不讨喜
生来不讨喜 2020-12-30 14:16

I\'m having a problem with Spring and a post request. I\'m setting up an controller method for an Ajax call, see the method definition below

@RequestMapping         


        
相关标签:
1条回答
  • 2020-12-30 15:03

    The problem turned out to be the way I was calling the method. My ajax code was passing all the parameters in the request body and not as request parameters, so that's why my @RequestParam parameters were all empty. I changed my ajax code to:

    $.ajax({
        type: 'POST',
        url: "/comments/add.page?uuid=${param.uuid}&type=${param.type}",
        data: text,
        success: function (data) {
            //
        }
    });
    

    I also changed my controller method to take the text from the request body:

    @RequestMapping(value = "add.page", method = RequestMethod.POST)
    @ResponseBody
    public Object createComment(
            @RequestParam(value = "uuid", required = false) String entityUuid,
            @RequestParam(value = "type", required = false) String entityType,
            @RequestBody String text,
            HttpServletResponse response) {
    

    And now I'm getting the parameters as I expect.

    0 讨论(0)
提交回复
热议问题