Search code examples
javarestform-parameter

REST @FormParam values are null


I have the following in 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

@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. What could be the reason for this?


Solution

  • Issue is rather silly,I was using name instead of id and that resulted in getting null in server side for all form elements. I have changed to

    <form id="myform" method="POST"> it works well

    However <form name="myform" method="POST"> doesn't work.