简体   繁体   English

使用 Jquery Z3EB7106C3477A590E6BBF5DBFDE 在 Spring MVC controller 中传递字符串列表

[英]Passing a list of strings in a Spring MVC controller using Jquery Ajax

i'm trying to pass a list of Strings to an MVC controller using JQuery Ajax, but i get the error No mapping for POST /myUrl/myContext/p我正在尝试使用 JQuery Ajax 将字符串列表传递给 MVC controller,但我收到错误No mapping for POST /myUrl/myContext/p

this is my jquery function:这是我的 jquery function:

$('#myButton').on('click', function(){
    var strings = [];
    $('#myForm input[type=checkbox]:checked').each(function(){
        var string = $(this).closest('tr').find('#mySpan').text();
        strings.push(string);
    });

    $.ajax({
        url : 'myContext/p',
        dataType : 'json',
        type: 'POST',
        data : {strings : strings},
        success: function(response) {
            //my success function           
            }
        },
        error: function(e) {
            //my error function
            }   
        }
    });

})

this is my controller:这是我的 controller:

@PostMapping(value="/myContext/p")
    public ResponseEntity<MyResponse> doPost(
            @RequestParam(value="strings" ,required=true) List<String> strings)
                    throws Exception{
        MyResponse response = new MyResponse();

        //my Code

        response.setData(strings);
        return new ResponseEntity<MyResponse>(response, HttpStatus.OK);
    }

I normally would use @RequestBody instead of @RequestParam for the strings parameter.我通常会使用 @RequestBody 而不是 @RequestParam 作为字符串参数。

I fixed the issue with:我解决了这个问题:

@PostMapping(value="/myContext/p", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, 
            produces = {MediaType.APPLICATION_ATOM_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<MyResponse> doPost( ArrayList<String> strings) throws Exception{
        MyResponse response = new MyResponse();

        //code

        response.setData(strings);
        return new ResponseEntity<RestResponse>(response, HttpStatus.OK);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM