정나영
정나영

Reputation: 65

I have a problem sending json data to a controller with ajax

I have a problem sending json data to a controller with ajax.

I think I sent the data well but I get the following warn.

org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required int parameter 'bno' is not present]

code:400
message:HTTP Status 400 – Bad Requesth1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}

HTTP Status 400 – Bad Request

Type Status Report

Message Required int parameter 'bno' is not present

Description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

Apache Tomcat/8.5.34

I'll show my ajax code

var headers = {"Content-Type" : "application/json"
        ,"X-HTTP-Method-Override" : "DELETE"
      };
$.ajax({
          url: root+"/restcmt/"+uid+"/"+cno
              , headers: headers
              , type: 'DELETE'
              , data : JSON.stringify({"bno":bno})
              , beforeSend : function(xhr){
                    xhr.setRequestHeader(_csrf_name, _csrf_token);
                }
              , success: function(result){
                  showcmtlist(bno);
              }
              , error: function(request,status,error){
                console.log("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);


                }
          });

And my controller

    @RequestMapping(value="/{uid}/{cno}", method=RequestMethod.DELETE)
    public void deletecmt(@PathVariable int cno ,@PathVariable String uid,@RequestParam int bno
            ,@AuthenticationPrincipal SecurityCustomUser securityCustomUser) throws Exception{

    }

and request payload

{"bno":14}

I'm not sure what's wrong. What's wrong?

Upvotes: 2

Views: 217

Answers (2)

mrak
mrak

Reputation: 2906

In the Spring-World request payload should correspond to @RequestBody, e.x:

public SomethingElse updateValue(@RequestBody Something value) {
    // ...
}

Where "Something" is any POJO.

In order to use @RequestParam, see: https://github.com/jquery/jquery/issues/3269

($.ajax Sends data property in DELETE body instead of query string)

Upvotes: 1

Kyler Love
Kyler Love

Reputation: 1021

{"bno": bno} is in the body of the request. So your Controller method should be @RequestBody int bno. @RequestParam is for servlet request parameters. i.e: /uid/cno?bno=14

difference for reference: What is difference between @RequestBody and @RequestParam?

Upvotes: 3

Related Questions