使用$ http.get使用AngularJS的RESTful调用返回错误

时间:2014-07-09 18:51:16

标签: javascript angularjs rest

我试图使用AngularJS调用我的RESTful服务。这是我的HTML

<!DOCTYPE html>
<html ng-app>
<head>
    <title>Hello World, AngularJS - ViralPatel.net</title>
    <script src="../js/angular.min.js"></script>
    <script src="../js/helloRest.js"></script>

</head>
<body>

    Write some text in textbox:
    <input type="text" ng-model="sometext" />

    <h1>Hello {{ sometext }}</h1>

    <h4>Uppercase: {{ sometext | uppercase }}</h4>
    <h4>Lowercase: {{ sometext | lowercase }}</h4>

    <div ng-controller="hello">
            <p>The ID is {{greeting.date}}</p>
            <p>The content is {{greeting.total}}</p>
    </div>

    <button type="button" onclick="hello()">Click Me!</button>

</body>
</html>

这是我的javascript:

function hello($scope, $http) {
    alert("HelloRest");
    $http.get('http://vxen102:9999/calcrest/calc/getServiceTimeList').
        success(function(data) {
            alert("ByeRestSuccess");
            alert(data);
            $scope.greeting = data;
        })
        .error(function(data) {
            alert("ByeRestError");
            alert(data);
        });
}

这是我的服务方式:

@GET
@Path("/getServiceTimeList")
@Produces(MediaType.APPLICATION_JSON)
public ServiceCountSnapshot getServiceTimeList(@PathParam("a") double a, @PathParam("b") double b) {
    System.out.println("Hit");

    ServiceCountSnapshot snapshot = new ServiceCountSnapshot();
    snapshot.setDate(new Date());
    snapshot.setDistributed(4);
    snapshot.setMainframe(5);
    snapshot.setTotal(9);
    return snapshot;
}

这是我试图返回的对象:

@XmlRootElement
public class ServiceCountSnapshot {

    Date date;
    int total;
    int mainframe;
    int distributed;

    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    public int getTotal() {
        return total;
    }
    public void setTotal(int total) {
        this.total = total;
    }
    public int getMainframe() {
        return mainframe;
    }
    public void setMainframe(int mainframe) {
        this.mainframe = mainframe;
    }
    public int getDistributed() {
        return distributed;
    }
    public void setDistributed(int distributed) {
        this.distributed = distributed;
    }


}

如果我直接从Chrome点击我的服务网址,则会显示我的预期:

{"serviceCountSnapshot":{"date":"2014-07-09T13:54:41.894-04:00","distributed":4,"mainframe":5,"total":9}}

然而,当我使用AngularJS尝试它时,它会遇到错误回调函数。我知道我的REST服务受到了影响,因为控制台显示了System.out.println(&#34; Hit&#34;)我放在那里。

为什么Get调用会导致错误?感谢。

0 个答案:

没有答案