简体   繁体   中英

How to pass array variable from view to controller in spring mvc

I want to pass the variable from view to controller I am using ajax call to achieve it i am getting the error below. I don't know what i am missing here.

WARN 41440 --- [nio-8080-exec-9] osweb.servlet.PageNotFound : Request method 'POST' not supported

This is my code

document.getElementById('btntest').onclick = function(){
  var selchbox = getSelectedChbox(this.form);     // gets the array returned by getSelectedChbox()
  myvalue = JSON.stringify(selchbox);
 //document.write("check check"+selchbox);
  $.ajax({
        type: "POST",
        url: "UserController/delete",
        contentType: "application/json; charset=utf-8",
        data: {key:myvalue},
        cache: false,
        success: function (data) {
                     alert("Are you sure?");
                 },
                 error: function (args) {
                     alert("Error on ajax post");
                 }

    });
  alert(selchbox);
}

My controller method looks like below

@RequestMapping(value = "/delete", method = RequestMethod.POST) 
public String delete(@RequestBody String key) {
    System.out.println("My Array value"+key.toString());
    return key;
}

What i am missing here? Any Help

At leaset two problems

  1. url: "UserController/delete" in your ajax won't match "/delete/{sORGID}" in your controller.
  2. data: {key:myvalue} in your ajax, the property name is key, in your controller it's myvalue[], this should also be the same.

First, if you want to delete, why not use the verb delete http?

I think you are not using the correct parameter: RequestParam is used to map your sORGID parameter to the URL (a parameter you did not use on the client side, you must use it or delete it). If you want to map Json, you must use @RequestBody.

I hope it helps

Finally i could able to pass the values from my view to controller I am posting the code. This is my js code

document.getElementById('btntest').onclick = function(){
  var selchbox = getSelectedChbox(this.form);     // gets the array returned by getSelectedChbox()
var myvalue = JSON.stringify(selchbox);
 //document.write("check check"+selchbox);
  $.ajax({
        type: "POST",
        url: "/delete",
        dataType : "JSON",
        contentType:"application/json; charset=utf-8",
        data: JSON.stringify(selchbox),
        cache: false,
        success: function (data) {
                     alert("Are you sure?");
                 },
                 error: function (args) {
                     alert("Error on ajax post");
                 }

    });
  alert(selchbox);
}

And my controller code

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
     public String delete(@RequestBody String value){
     System.out.println("My Array value"+value.toString());
     return value;
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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