JQuery Ajax POST 调用内容类型为 JSON 的 Jersey 无法执行

标签 jquery ajax json post jersey

我在使用 jQuery 的 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”字符串只是虚拟 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”相同。

我可以使用 Firefox 的 RESTClient 插件成功调用其余服务。我添加了一个“Content-Type: application/json” header ,正文与我从 jQuery 发送的 json 相匹配,并且成功,这表明该服务工作正常。如果我省略“Content-Type” header ,我会收到“415 Unsupported Media Type”(正确)。

为了确保服务正常工作,实际上这是一个“contentType:“application/json””问题,我通过需要纯文本的调用重新部署了我的服务:

@POST
@Path("/items")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public Response createItem(@Context final ServletContext context, String body)
{
 ...
}

并为post调用设置'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

有谁知道为什么将 ajax POST 调用的“contentType”设置为“application/json”会导致它根本不执行?

感谢您的时间和帮助!

最佳答案

是否是因为您发送的是 JSON 对象的字符串化版本,而不仅仅是对象?尝试改变:

data: JSON.stringify(postDataItem)

data: postDataItem

关于JQuery Ajax POST 调用内容类型为 JSON 的 Jersey 无法执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22566634/

相关文章:

javascript - 使用 Value() 方法从 Firebase 检索对象

json - 创建后将值附加到 JSON 结构

jQuery 1.3 只选择第一个元素

javascript - 在变量旁边的 URL 中动态添加路径。如何将其移到变量之前?

javascript - 我有一行,但不知何故我的按钮和文本区域在彼此下方而不是彼此相邻对齐

php - jquery:ajax 请求不起作用

php - 将 ajax 加载更多按钮添加到我的首页

PHP 和 Jquery 的 json

c# - 从后面的代码添加类属性

php - 通过 PHP 将 json 对象的列插入新列