[英]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.