我对AngularJS还很陌生,尝试按照AngularJS网站文档中给出的$http.get示例进行操作。
我有一个REST服务,当调用它时,返回的数据如下:
http://abc.com:8080/Files/REST/v1/list?&filter=FILE
{
"files": [
{
"filename": "a.json",
"type": "json",
"uploaded_ts": "20130321"
},
{
"filename": "b.xml",
"type": "xml",
"uploaded_ts": "20130321"
}
],
"num_files": 2}
我的 index.html 文件的一部分内容如下所示:
<div class="span6" ng-controller="FetchCtrl">
<form class="form-horizontal">
<button class="btn btn-success btn-large" ng-click="fetch()">Search</button>
</form>
<h2>File Names</h2>
<pre>http status code: {{status}}</pre>
<div ng-repeat="file in data.files">
<pre>Filename: {{file.filename}}</pre>
</div>
我的js文件如下所示:
function FetchCtrl($scope, $http, $templateCache) {
$scope.method = 'GET'; $scope.url = 'http://abc.com:8080/Files/REST/v1/list?&filter=FILE';
$scope.fetch = function() {
$scope.code = null;
$scope.response = null;
$http({method: $scope.method, url: $scope.url, cache: $templateCache}).
success(function(data, status) {
$scope.status = status;
$scope.data = data;
}).
error(function(data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
});
};
}
但是当我运行它时,我看不到任何关于文件名的结果,并且我看到http状态码为0
当我在浏览器中运行
http://abc.com:8080/Files/REST/v1/list?&filter=FILE时,我仍然可以看到期望的结果(如上所述)
我甚至尝试使用火狐浏览器中的Firebug进行调试,当我点击“搜索”按钮时,我看到上面的URL被调用,但响应似乎为空。有趣的是,在Firebug下的URL下,它显示
OPTIONS "上面的URL"
而不是
GET "上面的URL"
请告诉我,我做错了什么以及为什么我无法访问JSON数据?
谢谢,