[英]POST REST Service does not respond to my ajax jquery call (GET yes, it does)
I have got a simple rest server by jersey (java) with web interface that use jquery. 我有一个简单的rest jerseys by jersey(java),它具有使用jquery的Web界面。 I have a REST Service for GET and POST mode.
我有一个GET和POST模式的REST服务。 The POST call does not work, it has a json object in input data side and return a json data.
POST调用不起作用,它在输入数据侧具有json对象,并返回json数据。
Exception message: 异常消息:
A message body reader for Java class ....RESTTaskDataInput, and Java type class ....RESTTaskDataInput, and MIME media type application/x-www-form-urlencoded; charset=UTF-8 was not found.
The registered message body readers compatible with the MIME media type are:
application/x-www-form-urlencoded; charset=UTF-8 ->
com.sun.jersey.core.impl.provider.entity.FormProvider...
Jquery code for post call (a click button does the call): 帖子调用的jQuery代码(单击按钮可进行调用):
$("#task-launcher-post").click(function()
{
$("div#result").text("");
$.ajax({
type: "POST",
url: "resources/foo", <= This is OK, the rest interface is under that
data: {
code: "1234",
email: "my@email.com"
},
dataType: "json",
success: function(result)
{
$("div#result").append('...');
},
error: function(xml,result)
{
$("div#result").append('...');
}
});
});
Java REST Service code Java REST服务代码
@Path("/foo")
public class RESTSERVICECrawler
{
...
@POST
@Consumes("application/json")
@Produces("application/json")
public RESTLaunchResult launch(RESTTaskDataInput input)
{
RESTLaunchResult result = new RESTLaunchResult();
...
return result;
}
} }
Java RESTTaskDataInput code (data used in input side) Java RESTTaskDataInput代码(输入侧使用的数据)
@XmlRootElement
public class RESTTaskDataInput
{
public String code;
public String email;
}
A GET call works with url parameters, but POST does not with json in the input. GET调用适用于url参数,但POST不适用于输入中的json。 I dont know why, i read jquery documentation of ajax, I think that rest server cannot understand the json input data.
我不知道为什么,我阅读了ajax的jquery文档,我认为rest服务器无法理解json输入数据。
Try: 尝试:
public RESTLaunchResult launch(JAXBElement<RESTTaskDataInput> input) {
try {
RESTTaskDataInput data = input.getValue();
} ...
}
Solution thank to Charlie indication. 解决方案感谢查理的指示。 Is needed to set the contentType.
需要设置contentType。
var data = {
code: "123",
email: "email"
}
$.ajax({
type: "POST",
url: "resources/foo",
data: JSON.stringify(data),
dataType: "json",
contentType: 'application/json',
success: function(result)
{
$("div#result").append('<h5 id="result" class="text-success">' + result.success + " " + result.message + '</h5></div>');
},
error: function(xml,result)
{
$("div#result").append('<h5 id="result" class="text-error">' + result + '</h5>');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.