无法从AngularJS调用RESTFul服务

时间:2015-04-09 07:17:51

标签: angularjs jsonp

希望有些人可以帮助我。

我有一个宁静的服务,我从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

我也试过返回类型为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.

1 个答案:

答案 0 :(得分:0)

你的回调需要字面意思是“JSON_CALLBACK”。

尝试

$http.jsonp("http://localhost:8080/PMPortalService/rest/GetDataService?callback=JSON_CALLBACK")
相关问题