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 传递正确的值,产品也是空的。这可能是什么原因呢?

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) {
.....
.....

问题相当愚蠢,我使用 name 而不是 id,这导致所有表单元素在服务器端都为 null。我已更改为

<form id="myform" method="POST"> 它工作得很好

然而 <form name="myform" method="POST">不起作用。

最新更新