1

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] o.s.web.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

1
  • What does {sORGID} in your mapping means? Commented Oct 30, 2018 at 6:38

3 Answers 3

1

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;
     }
Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

I want to delete the selected checkbox value from db as well. In my js function "selchbox " is returning the selected checkbox value i am trying to pass this value to controller to perform delete action
@SKH So you send a json to your backend. To read it you need to change your annotation RequestParam to RequestBody. But you still will have a problem because the other parameter sORGID in your controller is not set in your js. Your url should looks like UserController/delete/mysORGID in your js. (BTW are you sure this is the correct url? )
I have edited the controller code as mentioned by you. Yes the url is correct(UserController is the controller name and delete is the method declared in the controller)
By default, it does not use the controller name for the URL. Did you explicitly put a RequestMapping on your controller with the value "UserController"?
0

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.

1 Comment

@SKH you can post the error message here. the warn 41440 is not the error

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.