[英]REST @FormParam values are null
我在HTML中有以下内容
<form name="myform" method="POST">
<input type="text" id="products" name="products" />
</form>
function myForm() {
var url = 'rest/products/details/';
var formData = $("#myform").serializeArray();
$.ajax({
url: url,
type: 'POST',
contentType : "application/x-www-form-urlencoded",
dataType: 'json',
data: formData,
success: function (data) {
//callfunc(data);
}
});
}
在Java服务器端,我有以下内容
@POST
@Path("/details")
public List<Product> findProducts(@FormParam("products") String products) {
.....
.....
log.info("prod "+products); --> getting null
由于某些原因,即使我从html传递了正确的值,产品也为null。 这可能是什么原因?
function myForm() {
var url = 'rest/products/details/';
var formData = "products=asasa" ;
$.ajax({
url: url,
type: 'POST',
contentType : "application/x-www-form-urlencoded",
dataType: 'json',
data: formData,
success: function (data) {
//callfunc(data);
}
});
}
试试这个并删除@consumes注解。 问题出在jquery的$("#myform").serializeArray()
函数中。
尝试Consumes
注释
@POST
@Path("/details")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public List<Product> findProducts(@FormParam("products") String products) {
.....
.....
问题相当愚蠢,我使用名称代替id,导致服务器端所有表单元素都为空。 我已更改为
<form id="myform" method="POST">
效果很好
但是<form name="myform" method="POST">
不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.