[英]How to post a json array to java servlet with jquery
I'm currently developing spring mvc
application and I need to post JSON array
. 我目前正在开发
spring mvc
应用程序,我需要发布JSON array
。
When I access request.getParameter("paramValue")
to fetch the param attibute, but it returning a null
value, 当我访问
request.getParameter("paramValue")
来获取param attibute时,它返回一个null
值,
Here is my front-end code: 这是我的前端代码:
$.ajax(url, {
async: true,
type: 'post',
contentType: 'application/json',
data: JSON.stringify({
"test":"test value"
})
}).done(function (response) {
console.log(data);
}).fail(function (xhr) {
console.log("request failed");
console.log(xhr);
});
Here is my server-side code: 这是我的服务器端代码:
@RequestMapping(value = "/Products", method = RequestMethod.POST)
public void saveProducts(HttpServletRequest req, HttpServletResponse res) throws Exception {
System.out.println(req.getContentType());
System.out.println(req.getContentLength());
System.out.println(req.getContextPath());
System.out.println(req.getParameterValues("test"));
System.out.println(req.getMethod());
StringBuilder buffer = new StringBuilder();
BufferedReader reader = req.getReader();
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String data = buffer.toString();
System.out.println(data);
System.out.println(req.getParameter("test"));
}
The output is: 输出是:
application/json
22
null
POST
{"test" : "Test DAta"}
null
I can't figure out whats going on, please help me. 我无法弄清楚发生了什么,请帮助我。
remove this line in you ajax function 在你的ajax函数中删除这一行
contentType: 'application/json',
and replace this line 并替换此行
data: JSON.stringify({
"test":"test value"
})
with 同
data: {
"test":"test value"
}
and also you can use 而你也可以使用
req.getParameter("test")
instead 代替
req.getParameterValues("test")
You can by using this : 你可以使用这个:
var data ={id: 1, name :'test'}
$.ajax(url, {
async: true,
type: 'post',
contentType: 'application/json',
data: data
}).done(function (response) {
console.log(data);
}).fail(function (xhr) {
console.log("request failed");
console.log(xhr);
});
and in server side create a pojo : 并在服务器端创建一个pojo:
public class Product{
private long id;
private String name;
// getters and setters
add library jackson . 添加库杰克逊。
add this method in your controller: 在您的控制器中添加此方法:
@RequestMapping(value = "/Products", method = RequestMethod.POST)
public RepsoneEntity<? >saveProducts(@requestBody Product pr){
LOG.debug(pr.toString());
return new reRepsoneEntity<Product>(pr,HttpStatus.ACCEPTED);
}
i finally fixed it several annotation and changing the the return type of server side method, 我最后修改了几个注释并更改了服务器端方法的返回类型,
@RequestMapping(value = "/Products", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public ResponseEntity<?> saveProducts(@RequestParam(value = "data") String brand) { return ResponseEntity.ok(brand); }</code>
front end $.ajax(url, { async: true, type: "POST", data: {"data" : JSON.stringify({"Brand" : "Test Brand"})}
public ResponseEntity<?> saveProducts(@RequestParam(value = "data") String brand) { return ResponseEntity.ok(brand); }</code>
front end $.ajax(url, { async: true, type: "POST", data: {"data" : JSON.stringify({"Brand" : "Test Brand"})}
public ResponseEntity<?> saveProducts(@RequestParam(value = "data") String brand) { return ResponseEntity.ok(brand); }</code>
front end $.ajax(url, { async: true, type: "POST", data: {"data" : JSON.stringify({"Brand" : "Test Brand"})}
public ResponseEntity<?> saveProducts(@RequestParam(value = "data") String brand) { return ResponseEntity.ok(brand); }</code>
front end $.ajax(url, { async: true, type: "POST", data: {"data" : JSON.stringify({"Brand" : "Test Brand"})}
}).done(function (response) { console.log(response); }).fail(function (xhr) { console.log("request failed"); console.log(xhr); });
and i used org.json to access json objects parsed as text, gson to deal with POJOs 我使用org.json来访问解析为文本的json对象,使用gson来处理POJO
and now it works :) 现在它的工作原理:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.