简体   繁体   中英

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?

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. the problem is in the $("#myform").serializeArray() function from jquery.

Try Consumes annotation

@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. I have changed to

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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