我正在尝试通过在控制台中记录来从JSON输出中访问数据,但控制台说该元素未定义。
我有一个有两个功能的控制器。两者都从TheMovieDB.org数据库发出http请求。
第一个函数执行与用户输入相对应的搜索查询。这工作正常,text
在这里工作,我看到所有返回的对象。
console.log
然后我在模板中显示该搜索的结果,
.controller('searchCtrl', [
'$scope', 'movieService', 'createMovie', 'removeMovie', '$http', function($scope, movieService, createMovie, removeMovie, $http) {
$scope.search = function() {
var base = 'http://api.themoviedb.org/3';
var service = '/search/movie';
var apiKey = 'a8f7039633f2065942cd8a28d7cadad4&query='
var search = $scope.searchquery
var callback = 'JSON_CALLBACK'; // provided by angular.js
var url = base + service + '?api_key=' + apiKey + search + '&callback=' + callback;
$http.jsonp(url,{ cache: true}).
success(function (data, status, headers, config) {
if (status == 200) {
$scope.movieList = data.results;
console.log($scope.movieList)
} else {
console.error('Error happened while getting the movie list.')
}
}).
error(function (data, status, headers, config) {
console.error('Error happened while getting the movie list.')
});
}
正如您所看到的,我对%ul#showresults
%li.search_results{"ng-repeat" => "movie in movieList | orderBy:'-release_date'"}
.addmovie{"ng-click" => "addMovie()"}
%span
Add Movie
%img.poster{"ng-src" => "http://image.tmdb.org/t/p/w500/{{ movie.poster_path }}"}
%span.movieID
{{ movie.id }}
%span.title
{{ movie.original_title }}
%span.release
{{ movie.release_date }}
函数有所了解。当用户点击此元素时,触发与搜索功能相同的控制器中的addMovie()
功能。
addMovie
它从用户想要添加的电影中获取ID并创建确实发生的http请求。它在网络选项卡中显示正确的链接。但控制台日志显示 $scope.addMovie = function() {
'http://api.themoviedb.org/3/movie/206647?api_key=a8f7039633f2065942cd8a28d7cadad4&append_to_response=releases'
// Search for release dates using the ID.
var base = 'http://api.themoviedb.org/3/movie/';
var movieID = $(event.currentTarget).parent().find('.movieID').text()
var apiKey = 'a8f7039633f2065942cd8a28d7cadad4&query='
var append_to_response = '&append_to_response=releases'
var callback = 'JSON_CALLBACK'; // provided by angular.js
var url = base + movieID + '?api_key=' + apiKey + append_to_response + '&callback=' + callback;
$http.jsonp(url,{ cache: true}).
success(function (data, status, headers, config) {
if (status == 200) {
$scope.movieListID = data.results;
console.log('succes!' + $scope.movieListID)
} else {
console.error('Error happened while getting the movie list.')
}
}).
error(function (data, status, headers, config) {
console.error('Error happened while getting the movie list.')
});
};
。成功是获取JSON数据,undefined是scope.MovieListID。
所以问题是,为什么这个succes! undefined
变得不确定?
答案 0 :(得分:2)
我在浏览器中打开了api链接。返回的JSON没有根密钥结果。
更改
$scope.movieListID = data;
到
@(NO)