我遇到使用jQuery&#39的ajax调用将JSON数据发布到我的REST服务的问题。
背景:
使用jQuery 1.9.1
使用Firefox 27.0.1(虽然在其他早期版本中也可以看到)
使用Jersey 1.1.5.1
我试图在Jersey REST服务中调用此方法:
@POST
@Path("/items")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public Response createItem(@Context final ServletContext context, String body)
{
...
}
我的jQuery ajax调用如下。请注意,' postData' string只是虚拟的json。我将下一行转换为对象,以确保我有有效的JSON。
var callPost = function() {
var postUrl = "https://otherhost:8443/package/service/items";
var postData = '{"TEST":true,"success":false}';
var postDataItem = JSON.parse(postData);
console.info("Post Data Item as string: " + JSON.stringify(postDataItem));
console.info("create post url: " + postUrl);
var ajaxPostCall =
$.ajax( { type: "POST",
url: postUrl,
data: JSON.stringify(postDataItem),
contentType: "application/json",
success:
function (data, textStatus, jqXHR)
{
var statusC = jqXHR.status;
console.log("Done Status Code: " + statusC);
console.log("Done textStatus: " + textStatus );
console.log("Done data: " + JSON.stringify(data) );
console.log("Item Fetch Called");
},
error:
function ( jqXHR, textStatus, errorThrown)
{
var responseText = jqXHR.responseText;
console.log("Fail responseText: " + responseText);
var statusC = jqXHR.status;
console.log("Fail Status Code: " + statusC);
console.log("Fail textStatus: " + textStatus );
console.log("Fail error: " + errorThrown );
}
});
};
当我执行此调用时,这是我在Firebug控制台中看到的内容:
回应1:
(info) Post Data Item as string: {"TEST":true,"success":false}
(info) create post url: https://otherhost:8443/package/service/items
Fail responseText:
Fail Status Code: 0
Fail textStatus: error
Fail error:
'回应1'让我相信ajax调用永远不会实际发送到服务器。如果我删除了' contentType:" application / json"'行,然后这是控制台显示的内容:
回应2:
(info) Post Data Item as string: {"TEST":true,"success":false}
(info) create post url: https://otherhost:8443/package/service/items
POST https://otherhost:8443/package/service/items 415 Unsupported Media Type 18ms
"NetworkError: 415 Unsupported Media Type - https://otherhost:8443/package/service/items"
Fail responseText:
Fail Status Code: 0
Fail textStatus: error
Fail error:
'回应2'显示调用已执行且服务器拒绝了该调用,因为该类型未成功设置为" json"。似乎由于某种原因,jQuery.ajax不允许将contentType设置为" application / json"。为了测试这一点,我设置了' contentType:" text / plain"'并且响应与响应2'相同。
我能够成功使用RESTClient插件为firefox调用其余服务。我为" Content-Type:application / json"添加标题。并且正文与我从jQuery发送的json匹配并且它成功,这向我显示该服务正常工作。我得到了一个不支持的媒体类型' (正确)如果我遗漏了内容类型'。
的标题为了确保服务正常运行,实际上这是一个' contentType:" application / json"'问题,我通过预期明文的电话重新部署了我的服务:
@POST
@Path("/items")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public Response createItem(@Context final ServletContext context, String body)
{
...
}
并设置' contentType:" text / plain"'对于电话会议,并成功地取回了这个:
回应3:
(info) Post Data Item as string: {"TEST":true,"success":true}
(info) create post url: https://otherhost:8443/package/service/items
POST https://otherhost:8443/package/service/items 200 OK 297ms
Done Status Code: 200
Done textStatus: success
Done data: "Success creating Item: newItemId"
Item Fetch Called
'回应3'按预期回来并成功。
我测试了坏json并成功从服务中找回了错误。
回应4:
(info) Post Data Item as string: {"TEST":true,"success":false}
(info) create post url: https://otherhost:8443/package/service/items
POST https://otherhost:8443/package/service/items 500 Internal Server Error 142ms
"NetworkError: 500 Internal Server Error - https://otherhost:8443/package/service/items"
Fail responseText: Error creating Item: null
Fail Status Code: 500
Fail textStatus: error
Fail error: Internal Server Error
有谁知道为什么要设置' contentType'对于ajax POST调用' application / json'会导致它根本不执行吗?
感谢您的时间和帮助!
答案 0 :(得分:0)
可能是因为您发送的是JSON对象的字符串化版本而不仅仅是对象?尝试更改:
data: JSON.stringify(postDataItem)
到
data: postDataItem