1

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

this is my 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:

@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);
    }

2 Answers 2

1

I normally would use @RequestBody instead of @RequestParam for the strings parameter.

Sign up to request clarification or add additional context in comments.

1 Comment

Also, check your url in your mapping. Your javascript has "myContext/p" without a leading slash, and the controller has a leading slash. In my js I normally preface my urls with ${pageContext.request.contextPath} in the jsp.
0

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);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.