0

我的 index.html 是:

<!DOCTYPE html>
<html lang="en" ng-app="myApp">

<head>
  <meta charset="utf-8">

  <title>HTTP Request</title>

  <script src="angularjs"></script>
  <script src="appjs"></script>

</head>
<body>

    <div ng-controller="myCtrl">

        Test here : <input ng-model="testString">
        <p>Test String is : {{testString}}</p>
        <button ng-click="search()">Send HTTP Request</button>
        <p>Response:</p>
        {{data}}
    </div>

</body>
</html>

我的 app.js 是:

angular.module('myApp', [])
    .controller('myCtrl', ['$scope', '$http', function($scope, $http) {
        $scope.testString = "Hello....";

        $scope.search = function() {
//          alert("inside search");
            $http.get('www.google.com', {},
                function(response) {
                    $scope.data = response;
                    alert("success");
                },
                function(failure) {
                    alert("failure");
            });
        };
    }]);

它正在根据警报(“内部搜索”)进入搜索()函数。但我既没有得到回应也没有失败。我应该在这里做什么?

4

2 回答 2

2

您需要根据Angular http 文档.success使用和.error检索结果,例如

$http.get('http://www.google.com').
    success(function(response) {
                $scope.data = response;
                alert("success");
            }).
    error(function(failure) {alert("failure")});
于 2013-09-20T05:42:25.740 回答
1
 $http.get('www.google.com', {},
            function(response) {
                $scope.data = response;
                alert("success");
            },
            function(failure) {
                alert("failure");
});

替换为:

$http.jsonp('http://www.google.com', {})
         .success(function(response, status) {
             $scope.data = response;
                alert("success");
        })
        . error(function(data, status) {
             alert("failure");
     });

演示

于 2013-09-20T05:49:39.010 回答