gpt4 book ai didi

java - 我在使用 ajax 将 json 数据发送到 Controller 时遇到问题

转载 作者:行者123 更新时间:2023-11-30 06:40:24 28 4
gpt4 key购买 nike

我在使用 ajax 将 json 数据发送到 Controller 时遇到问题。

我认为我发送的数据很好,但我收到以下警告。

org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - 已解决 [org.springframework.web.bind.MissingServletRequestParameterException:必需的 int 参数 'bno' 不存在]

code:400message: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);


}
});

还有我的 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{

}

和请求负载

{"bno":14}

我不确定哪里出了问题。怎么了?

最佳答案

{"bno": bno} 在请求的正文中。所以你的 Controller 方法应该是@RequestBody int bno@RequestParam 用于 servlet 请求参数。即:/uid/cno?bno=14

引用差异:What is difference between @RequestBody and @RequestParam?

关于java - 我在使用 ajax 将 json 数据发送到 Controller 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58186365/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com