gpt4 book ai didi

java - REST @FormParam 值为空

转载 作者:行者123 更新时间:2023-12-01 11:25:18 24 4
gpt4 key购买 nike

我的 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() 函数中。

关于java - REST @FormParam 值为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30860391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com