[英]REST @FormParam is null
I have the following being passed from browser to server 我有以下从浏览器传递到服务器
Status Code:204 No Content
Request Method:POST
Content-Type:application/x-www-form-urlencoded
Form Data
json:{"clientName":"PACK","status":"success","message":"successful!"}
and in jsp code 并在jsp代码中
var loginData = {
clientName: cList,
status: "success",
message: "successful!"
};
$.ajax({
url: subUrl,
type: 'POST',
contentType : "application/x-www-form-urlencoded",
data: {
json: JSON.stringify(loginData)
},
success: function (data) {
handleLoginResult(data);
}
});
And in Java code I have 在Java代码中我有
@POST
public Object persistResetPasswordLogs(@FormParam("clientName")
String clientName) {
try {
log.info("in rest method ??? "+clientName);
.......
.......
In server I am getting clientName as null. 在服务器中,我将clientName作为null。
What could be the reason for this and how can I resolve this? 可能是什么原因,我该如何解决这个问题?
AFAIK, there is no Jersey (JAX-RS) mechanism to parse JSON into form data. AFAIK,没有Jersey(JAX-RS)机制将JSON解析为表单数据。 Form data should be in the form of something like
表格数据应该是类似的形式
firstName=Stack&lastName=Overflow (or in your case clientName=someName)
where firstName
and lastName
are generally then name
attribute value in the form input elements. 其中
firstName
和lastName
通常是表单输入元素中的name
属性值。 You can use jQuery to easily serialize the field values, with a single serialize()
method. 您可以使用jQuery轻松序列化字段值,使用单个
serialize()
方法。
So you might have something that looks more along the lines of something like 所以你可能会看到类似的东西
<form id="post-form" action="/path/to/resource">
Client Name: <input type="text" name="clientName"/>
</form>
<input id="submit" type="button" value="Submit"/>
<script>
$("#submit").click(function(e) {
$.ajax({
url: $("form").attr("action"),
data: $("form").serialize(),
type: "post",
success: processResponse,
contentType: "application/x-www-form-urlencoded"
});
});
function processResponse(data) {
alert(data);
}
</script>
Have you defined the Requestmapping like this: 你有没有像这样定义Requestmapping:
@POST
@Path("/submitclient") // your request mapping for 'subUrl'
public Object persistResetPasswordLogs(@FormParam("clientName") String clientName)
and html: 和HTML:
<form action="submitclient" method="post">
...
</form>
Also look at your json object. 还要看看你的json对象。 I believe you should send something like this:
我相信你应该发送这样的东西:
var loginData = {
clientName: "dit" // get it from input
};
? ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.