无法从 AngularJS 调用 RESTFul 服务
Not able to invoke RESTFul service from AngularJS
希望有人能帮到我。
我有一个 restful 服务,我正在从 AngularJs 应用程序调用它,但出现如下错误。我能够从浏览器或任何其他休息客户端调用该服务并能够获得结果。请查看以下代码并提供帮助。
我的控制器
scotchApp.controller('newLogicalTeamController', function($scope,$http,$routeParams) {
// create a message to display in our view
$scope.applicationSel = $routeParams.Appl_Sel;
console.log(1111);
$http.jsonp("http://localhost:8080/PortalService/rest/GetDataService?callback=callback")
.success(function(response) {
console.log(1);
$scope.ApplicationList = response;
console.log(response);
}).error(function(){
console.log(2);
});
});
我的服务
@GET
@JSONP(queryParam="callback")
@Produces({"application/x-javascript"})
public List<LogicalTeam> getLogicalTeams()
{
System.out.println("1");
con = PostgreSQLJDBC.getConnection();
try {
st = con.createStatement();
rs = st.executeQuery(QUERY_GET_LOGICAL_TEAM);
System.out.println("2");
while(rs.next())
{
System.out.println("3");
LogicalTeam team = new LogicalTeam();
team.setIntTeamId(rs.getInt(1));
team.setStrTeamName(rs.getString(2));
team.setStrSubBizGrp(rs.getString(3));
team.setStrBizGrp(rs.getString(4));
teamList.add(team);
}
return teamList;
//return new JSONWithPadding(teamList,callback);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
当我从浏览器点击服务时的输出。
callback([{"intTeamId":300,"strBizGrp":"100","strSubBizGrp":"201","strTeamName":"New Business"}])
控制台日志
Uncaught ReferenceError: callback is not defined
我也试过 return 输入 JSONPadding 但没有成功。当我使用 $http.get 时出现如下错误。所以我切换到 $http.jsonp。我不确定调用休息服务是否正确
XMLHttpRequest cannot load http://localhost:8080/PMPortalService/rest/GetDataService?callback=callback. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.
您的回调需要 "JSON_CALLBACK" 字面意思。
尝试
$http.jsonp("http://localhost:8080/PMPortalService/rest/GetDataService?callback=JSON_CALLBACK")
希望有人能帮到我。
我有一个 restful 服务,我正在从 AngularJs 应用程序调用它,但出现如下错误。我能够从浏览器或任何其他休息客户端调用该服务并能够获得结果。请查看以下代码并提供帮助。
我的控制器
scotchApp.controller('newLogicalTeamController', function($scope,$http,$routeParams) {
// create a message to display in our view
$scope.applicationSel = $routeParams.Appl_Sel;
console.log(1111);
$http.jsonp("http://localhost:8080/PortalService/rest/GetDataService?callback=callback")
.success(function(response) {
console.log(1);
$scope.ApplicationList = response;
console.log(response);
}).error(function(){
console.log(2);
});
});
我的服务
@GET
@JSONP(queryParam="callback")
@Produces({"application/x-javascript"})
public List<LogicalTeam> getLogicalTeams()
{
System.out.println("1");
con = PostgreSQLJDBC.getConnection();
try {
st = con.createStatement();
rs = st.executeQuery(QUERY_GET_LOGICAL_TEAM);
System.out.println("2");
while(rs.next())
{
System.out.println("3");
LogicalTeam team = new LogicalTeam();
team.setIntTeamId(rs.getInt(1));
team.setStrTeamName(rs.getString(2));
team.setStrSubBizGrp(rs.getString(3));
team.setStrBizGrp(rs.getString(4));
teamList.add(team);
}
return teamList;
//return new JSONWithPadding(teamList,callback);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
当我从浏览器点击服务时的输出。
callback([{"intTeamId":300,"strBizGrp":"100","strSubBizGrp":"201","strTeamName":"New Business"}])
控制台日志
Uncaught ReferenceError: callback is not defined
我也试过 return 输入 JSONPadding 但没有成功。当我使用 $http.get 时出现如下错误。所以我切换到 $http.jsonp。我不确定调用休息服务是否正确
XMLHttpRequest cannot load http://localhost:8080/PMPortalService/rest/GetDataService?callback=callback. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.
您的回调需要 "JSON_CALLBACK" 字面意思。
尝试
$http.jsonp("http://localhost:8080/PMPortalService/rest/GetDataService?callback=JSON_CALLBACK")