AngularJS:http请求不返回任何内容

时间:2016-04-07 16:35:51

标签: angularjs rest http

我想用输入框和按钮构建一个简单的查询页面。这是我的HTML:

<html ng-app="cgApp" >
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
    <script src="../js/controller.js"></script>
    <script src="../js/service.js"></script>

  </head>
  <body>
    <div ng-controller="CgseqCtrl">
        <input ng-model="analysisid"><button ng-click="searchById()">Search</button>
        <table>
            <tr>
                <td>{{seq.analysisId}}</td>
                <td>{{seq.center}}</td>
                <td>{{seq.diseaseAbbr}}</td>
                <td>{{seq.filepath}}</td>
                <td>{{seq.library}}</td>
            </tr>
        </table>

    </div>

  </body>
</html>

处理ng-click事件searchById()的函数在我的controller.js

中实现
var app = angular.module('cgApp', [])

app.controller('CgseqCtrl', ['$scope', 'Cgseq', function($scope, Cgseq){
    $scope.searchById = function() {
        Cgseq.getSeqById($scope.analysisid)
        .then(function(response){
            $scope.seq = response;
        }, function errorCallBack(response) {
            console.log(response.$statusText);
        });
    }
}]);

Cgseq是service.js中我工厂的名称。

app.factory("Cgseq", function ($http) {
    // return $resource('http://localhost:8080/cgweb/api/seqs/fdebfd6e-d046-4192-8b97-ac9f65dc2009');
    var service = {};
    service.getSeqById = function(analysisid) {
        return $http.get('http://localhost:8080/cgweb/api/seqs/' + analysisid);
    }
    return service;
});

当我加载basic.html时,输入查询字符串并单击按钮,页面中没有任何反应。我试图逐步调试,事实证明它从未进入.thenfunction errorCallBack。 node.js服务器或tomcat服务器的日志中没有任何内容可疑。我做错了什么?

编辑#1:

根据网络状态,看起来与服务器的连接是好的(200)。 network tab

1 个答案:

答案 0 :(得分:1)

Cgseq.getSeqById($scope.analysisid)
        .then(function(response){
            $scope.seq = response.data; // You have to use response.data
        }, function errorCallBack(response) {
            console.log(response.$statusText);
        });

参考:https://docs.angularjs.org/api/ng/service/ $ http

如果你在当时的回调中做console.log(response),你可以看到响应对象的结构。