【发布时间】:2013-04-22 07:44:38
【问题描述】:
我在发送 REST 调用时收到带有 JSON 响应数据的奇怪响应。我可以在萤火虫中看到响应状态还可以,但是主体是空的,即使尝试使用海报也可以正常工作。这是我的 JQuery 调用:
var urlPath = "http://localhost:8080/coreserver/rest";
function totalAccountCount()
{
$.ajax({
type: "GET",
url: urlPath + "/summary/totalAccountCount",
dataType: "json",
success: function (resp) {
var rowText = "<tr><td>Total Accounts</td><td>" + resp.wrapper.item + "</td></tr>";
$(rowText).appendTo(tbody);
//loadCustomers(resp);
alert("STATUS: " + xhr.status + " " + xhr.statusText);
},
error: function(xhr, textStatus, errorThrown){
alert("Error: fails");
//$("#message").html(resp.e + " - STATUS: " + xhr.status + " " + xhr.statusText);
}
});
}
在调试时,它并没有进入成功分支,而是进入错误分支。这是我的资源:
@GET
@Path("/totalAccountCount")
@Produces({MediaType.APPLICATION_JSON})
public JaxbWrapper<Integer> getTotalAccountCount() {
return new JaxbWrapper<Integer>((int)this.accountStore.count());
}
我已经知道用JaxbWrapper 阅读我的回复可能是错误的,但根本没有成功,所以现在我想确保它有效。
我将不胜感激。提前致谢。
【问题讨论】:
-
从 jQuery 1.9 开始,当使用
dataType:'json'时,如果服务器响应为空,则不视为成功。 -
我目前正在使用 jquery-1.8.3: status is ok 200 但实际上并没有成功
-
这是跨域请求吗?如果是这样可以获得 200 但如果没有启用 CORS 或设置 jsonp 将不会触发成功
-
不,它是本地主机,都在同一个域上。此外,当我尝试从返回值的浏览器调用它时,它与海报一起使用,但作为 XML 而不是 JSON,我不知道这是否可能是问题?
-
尝试比较原始请求,即从 jquery 执行的操作和从浏览器执行的操作。还可以尝试从您的 jquery 调用中设置内容类型。
标签: java jquery ajax json rest