- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试用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/
我是一名优秀的程序员,十分优秀!