简体   繁体   English

REST @FormParam值为null

[英]REST @FormParam values are null

I have the following in html 我在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);
        }
    });
}

In Java server side I have the following 在Java服务器端,我有以下内容

@POST
@Path("/details")
public List<Product> findProducts(@FormParam("products") String products) {
.....
.....

log.info("prod "+products); --> getting null

For some reason products is null even though I am passing correct values from html. 由于某些原因,即使我从html传递了正确的值,产品也为null。 What could be the reason for this? 这可能是什么原因?

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);
        }
    });
}

try this and remove @consumes annotation. 试试这个并删除@consumes注解。 the problem is in the $("#myform").serializeArray() function from jquery. 问题出在jquery的$("#myform").serializeArray()函数中。

Try Consumes annotation 尝试Consumes注释

@POST
@Path("/details")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public List<Product> findProducts(@FormParam("products") String products) {
.....
.....

Issue is rather silly,I was using name instead of id and that resulted in getting null in server side for all form elements. 问题相当愚蠢,我使用名称代替id,导致服务器端所有表单元素都为空。 I have changed to 我已更改为

<form id="myform" method="POST"> it works well <form id="myform" method="POST">效果很好

However <form name="myform" method="POST"> doesn't work. 但是<form name="myform" method="POST">不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM