0

I writing code to send data from javascript to a Spring MVC Controller using Ajax.

It appears that my Ajax code is working fine as my "success" message is printed in the client console, so I am assuming the POST was executed. However, my Controller doesn't seem to be getting called as its message is never printed to the server console.

My Ajax code:

<script>
            $(document).ready(function () {
                $("#submit-btn-2").on("click", function () {
                    var idList = [];
                    $(".vm-row").has(":checkbox:checked").each(function() {
                        idList.push($(this).attr('id'));
                    });
                    $.ajax({
                        type: "POST",
                        url: 'submitVendors',
                        data: {idList:idList},
                        success: function(id) {
                            console.log("SUCCESS: ", idList);                           
                        },
                        error : function(e) {
                            console.log("ERROR: ", e);
                        },
                        done : function(e) {
                            console.log("DONE")
                        }
                    })
                });
            });
</script>

Clicking the associated button causes this message to be printed to the client console:

SUCCESS:  Array(3)

My Controller:

@RequestMapping(value="/submitVendors", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody 
    String submitVendors(@RequestParam("idList[]") String[] idList, HttpServletRequest request) {

        String vid = request.getSession().getAttribute("vid").toString();
        System.out.println("Inside submitVendors service.");

        for (String id : idList)
        {
            System.out.println("It actually worked: " + id);
        }

        return "vendormanagement";
    }

No error of any sort is printed to the server console and neither are messages.

2 Answers 2

2

Try this :

data: JSON.stringify(idList), and (@RequestBody String[] idList)

OR this :

data: {"idList":idList.toString()}, and (@RequestParam(value = "idList") List<String> idList)

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

2 Comments

Unfortunately this does not fix my issue. I think I know what the problem is, I just don't know how to resolve. The ajax is being called in vm.jsp. It then sends the post to appname/vm/submitVendors. But I don't think that is where the Controller is supposed to be called at. It is very confusing.
check whether you are giving correct url path: $.ajax({ type: "POST", url: '/appname/controllerPath/submitVendors',
0

pass the RequestParam name mached with variable name return by ajax,

@RequestParam(value="idList") ArrayList<String> idList)

add/ change the following properties into/ in ajax function

dataType: 'json',
data:{idList : idList.toString()}

2 Comments

Unfortunately, changing the RequestParam name did not fix my problem. My System.out.println messages are still not being printed in server console. I need the HttpServletRequest request as it is being used to pass a variable to this Controller from a different Controller. It is not being used by my Ajax.
@RyanSwanson please share the vm.jsp file

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.