Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
$http响应在angularJS中未定义_Angularjs_Xmlhttprequest - Fatal编程技术网

$http响应在angularJS中未定义

$http响应在angularJS中未定义,angularjs,xmlhttprequest,Angularjs,Xmlhttprequest,我遇到了一些问题,我似乎在回调中获取数据,但似乎无法获取数据,老实说,我不完全确定发生了什么 当我单击“获取电影”按钮时,控制台中会显示以下内容: 错误:响应未定义 app在$video.search()方法中,您不返回任何内容,因此出现错误 您可以像这样返回$http()调用,它应该可以工作: angular.module('video', []).factory('$video', ['$http', function($http) { return { search: func

我遇到了一些问题,我似乎在回调中获取数据,但似乎无法获取数据,老实说,我不完全确定发生了什么

当我单击“获取电影”按钮时,控制台中会显示以下内容:

错误:响应未定义
app在
$video.search()
方法中,您不返回任何内容,因此出现错误

您可以像这样返回
$http()
调用,它应该可以工作:

angular.module('video', []).factory('$video', ['$http', function($http) {
  return {
    search: function(api_key, query, page_limit) {
      var method = 'JSONP';
      var url = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?";
      return $http({
        method: method,
        url: url + "apikey=" + api_key +
            "&q=" + query +
            "&page_limit=" + page_limit + '&callback=JSON_CALLBACK'
      });
    }
  };
}]);
一般提示:

  • 在调试时,使用未统一的
    angular.js
    而不是
    angular.min.js
    ,这会给您带来更多有意义的错误
  • 无需定义
    jsonp\u回调
    函数,angular将为您处理该函数
  • 避免使用
    $
    前缀命名您自己的服务,即
    $video
    。对于angularjs中内置的任何内容,都会保留
    $
    前缀

我想我使用$naming约定的唯一原因是为了模仿firebase,但用于其他服务。我必须让他们知道为什么他们使用$firebase。
<!DOCTYPE html>
<html ng-app="myapp" manifest="covers.appcache">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.6/angular.min.js"></script>
    <script src="https://cdn.firebase.com/js/client/1.0.11/firebase.js"></script>
    <script src="https://cdn.firebase.com/libs/angularfire/0.7.1/angularfire.min.js"></script>
    <script src="rottentomatoes.js"></script>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    <style type="text/css">
    <!--
      /* Move down content because we have a fixed navbar that is 50px tall */
      body {
        padding-top: 50px;
        padding-bottom: 20px;
      }
    -->
    </style>
    <!-- Optional theme -->
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
  </head>
  <body ng-controller="MyController">

    <div align="left">
       <button type="button" class="btn btn-primary btn-lg active" ng-click="addBook()">Get Movie</button>
    </div>
    <br />
    <div>
      {{data}}
      {{success}}

    <script>
      var app = angular.module("myapp", ["video"])
      .controller('MyController', ['$scope', '$video', function($scope, $video) {        
        $scope.addBook = function(e) {
          var response = $video.search('[api-key]', 'The Way Of The Gun', '1');

          response
            .success(function(data, status) {console.log('SUCCESS' + data); $scope.data = data;  $scope.status = status;})
            .error(function(data, status) {console.log('ERROR' + status); $scope.data = data; $scope.status = status;});

          // Found somewhere that said I needed a jsonp_callback function, I just tried it to see if it did anything, doesn't appear to
          function jsonp_callback(data) {console.log('JSONP' + data);  $scope.data = data; };
        }    
      }]);

      app.config(function($httpProvider) {
        //Enable cross domain calls
        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common['X-Requested-With'];
        $httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
      });
    </script>
  </body>
</html>
angular.module('video', []).factory('$video', ['$http', function($http) {
  return {
    search: function(api_key, query, page_limit) {
      var method = 'JSONP';
      var url = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?";
      $http({
        method: method,
        url: url + "apikey=" + api_key +
            "&q=" + query +
            "&page_limit=" + page_limit + '&callback=JSON_CALLBACK'
      });
    }
  };
}]);
angular.module('video', []).factory('$video', ['$http', function($http) {
  return {
    search: function(api_key, query, page_limit) {
      var method = 'JSONP';
      var url = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?";
      return $http({
        method: method,
        url: url + "apikey=" + api_key +
            "&q=" + query +
            "&page_limit=" + page_limit + '&callback=JSON_CALLBACK'
      });
    }
  };
}]);