我的 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 传递了正确的值,产品仍为空。这可能是什么原因?
最佳答案
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()
函数中。
关于java - REST @FormParam 值为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30860391/