Javascript 检索不使用AngularJS和GSON的JSON
我有一个RESTapi,使用以下方法:Javascript 检索不使用AngularJS和GSON的JSON,javascript,json,angularjs,resteasy,Javascript,Json,Angularjs,Resteasy,我有一个RESTapi,使用以下方法: @Path("/banksummary") public class BankSummaryRestService { Database db = new DatabaseAccessOutput(); @GET @Path("/") public String printMessage() throws Exception { Summary result; try {
@Path("/banksummary")
public class BankSummaryRestService {
Database db = new DatabaseAccessOutput();
@GET
@Path("/")
public String printMessage() throws Exception {
Summary result;
try {
result = db.getBankSummary();
}
catch (Exception e) {
throw e;
}
Gson gson = new Gson();
String json = gson.toJson(result);
return json;
}
}
当我转到localhost:8080/html5/api/1.0/banksummary
时,我看到了输出
在我的AngularJS项目中,我有以下代码:
在service.js中:
dashboardAPI.getBankSummary = function() {
return $http(
{
method: 'GET',
url: 'localhost:8080/html5/api/1.0/banksummary'
}
);
}
在控制器中:
dashboardAPIService.getBankSummary().success(function (response) {
$scope.summary = response;
});
当我使用本地json文件时,它工作正常,但现在我使用RESTapi的URL(本地文件必须被RESTapi替换),我得到以下错误:
XMLHttpRequest cannot load localhost:8080/html5/api/1.0/banksummary. Cross origin requests are only supported for HTTP. angular.min.js:76
Error: A network error occurred.
有人知道我做错了什么吗?您需要在URL中指定协议(在本例中为http):
dashboardAPI.getBankSummary = function() {
return $http(
{
method: 'GET',
url: 'http://localhost:8080/html5/api/1.0/banksummary'
}
);
}
是的,这确实是解决办法。