【发布时间】:2013-10-21 19:16:17
【问题描述】:
我正在尝试访问在 Java 上创建并在 Jersey 的帮助下使用 jQuery 部署的 RESTful 服务。
如果我使用浏览器访问它,我会得到结果,但是从 jQuery 中,我得到一个错误并且在页面上看不到任何结果。
带有脚本的页面托管在本地 Apache 服务器上,并且服务在同一台机器上使用 Jersey/Grizzly 单独运行。
我可以看到该服务正在发送响应并且它有 200 个代码,但我不断收到来自 .ajax 的错误,没有任何详细信息并且 有什么建议吗?
服务:
@Path("/helloworld")
公共类 HelloWorldResource {
@GET
@Produces
public String test(){
System.out.println("Sending response");
return "test";
}
}
主要:
public static void main(String[] args) throws IOException {
final String baseUri = "http://localhost:9998/";
final Map<String, String> initParams = new HashMap<String, String>();
initParams.put("com.sun.jersey.config.property.packages",
"resources");
System.out.println("Starting grizly");
SelectorThread threadSelector = GrizzlyWebContainerFactory.create(baseUri, initParams);
System.out.println(String.format(
"Jersey app started with WADL available at %sapplication.wadl\n"
+ "Try out %shelloworld\nHit enter to stop it...", baseUri, baseUri));
System.in.read();
threadSelector.stopEndpoint();
System.exit(0);
}
JavaScript:
var serviceAddress = "http://192.168.1.2:9998/helloworld";
function loadDeviceData(){
$.ajax({
DataType: "text",
url: serviceAddress,
success: function (data) {
alert("Data loaded: " + data);
},
error: function (xhr) {
alert(xhr.responseText + ' ' + xhr.status + ' ' + xhr.statusText);
}
});
}
【问题讨论】: