gpt4 book ai didi

java - 如何在 Spring 中发送和检索参数?

转载 作者:行者123 更新时间:2023-12-01 11:18:24 25 4
gpt4 key购买 nike

我正在尝试用ajax 做一件简单的事情,发送请求(使用GET 或POST)。

我将以 json 格式发送 2 个参数,我只想取回它们并发送响应,但我总是收到错误 400 和其他错误,我不知道出了什么问题,知道怎么办吗?

我是根据这篇文章开始的:http://fruzenshtein.com/spring-mvc-ajax-jquery/

我正在使用 spring mvc。

到目前为止我有这个:

$(".update_agent").live('click', function(){

var agent = { "agentId" : agentID, "hostAGent" : hostID};
//send ajax
$.ajax({
url: url,
data: JSON.stringify(agent),
type: "GET",
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
success: function(data) {
alert("success");
},
error: function(){
alert("error");
}
});
})

在我的 java Controller 上我有这个

@RequestMapping(value = "/update", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public int updateAgent(HttpServletRequest req, HttpServletResponse res) throws IOException{
req.getParameterValues("agentId");
return AGENT_UPDATE_SUCCESS;
}

但我无法取回它,不知道如何发出参数请求,知道吗?

谢谢。

======================更新========================== ===

我已经更改了代码,它看起来像这样......

$.ajax({
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
type: 'POST',
url: url,
data: JSON.stringify(agent),
dataType: 'json',
success:function(data) {
alert("success");
},
error: function(){
alert("error");
}
});

在我的 Controller

@RequestMapping(value = "/update", method = RequestMethod.POST)
public @ResponseBody Integer updateAgent(@RequestBody String param) throws IOException{
System.out.println(param);
//do something...
return 1;

}

问题是我收到错误 415,不支持的媒体类型,有什么建议吗?

最佳答案

GET 请求不能有“数据”字段。您需要将数据作为网址的一部分发送:

$.ajax({
url: url + "?agent=" + JSON.stringify(agent),
type: "GET",
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
success: function(data) {
alert("success");
},
error: function(){
alert("error");
}
});

现在您可以在 Controller 中获取数据:

@ResponseBody public ResponseEntity<String> updateAgent(@RequestParam(value = "agent") String agentJson){
...
}

或者您可以发送 POST 请求。通过 POST 请求,您可以将数据作为 requestBody 发送:

public @ResponseBody ResponseEntity<String> updateAgent(@RequestBody String agentJson){
...
}

编辑:创建一个新的Agent类:

public class Agent {
private long agentId;
private long hostAgent;
...
getter and setter
...
}

现在将 Controller 更新为:

public @ResponseBody ResponseEntity<String> updateAgent(@RequestBody Agent agent){
System.out.println(agent.getAgentId());
}

并将ajax调用的“Content-Type”更改为“application/json”。

编辑2:将您的 ajax 调用数据更改为:

data: { agentId: agentID, hostAgent : hostAgentID} ,

甚至

data: agent ,

不要忘记将代理对象中的“hostAGent”更改为“hostAgent”,否则您将得到 400!!!

现在ajax会将数据作为请求参数发送,您可以通过以下方式获取 Controller 中的数据:

public @ResponseBody ResponseEntity<String> updateAgent(@RequestParam(value = "agentId") long agentId, @RequestParam(value = "hostAgent") long hostAgentId){
System.out.println(agentId);
}

关于java - 如何在 Spring 中发送和检索参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31526384/

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