在AngularJS中使用$http.get检索JSON时出现错误400
Error 400 when retrieving JSON with $http.get in AngularJS
当我试图用$http.get.从下面的URL检索JSON时,我收到了一个错误400
$http.get('https://api.spotify.com/v1/search?q=artist:Owl+City+title:On+The+Wing&type=track&limit=1').
success(function(data) {
console.log("Success");
})
.error(function(error, status, headers, config) {
console.log(status);
console.log("Error occured");
});
我设法用这个检索它:
$.getJSON('https://api.spotify.com/v1/search?q=artist:Owl+City+title:On+The+Wing&type=track&limit=1').then(function (data) { console.log("Success");});
但问题是,当我试图用这个方法更改$scope中的值时,它只显示在视图中,一个操作之后,因为我得到的是以前的歌曲信息,而不是当前的。
这是控制器代码:
angular.module('pwilApp')
.controller('SongsCtrl', function ($rootScope, $scope,$http,$sce) {
var increment = 0;
var laSimilaire = "";
var init = true;
$rootScope.activeHome = "";
$rootScope.activeSongs = "active";
$rootScope.activeAccount = "";
$rootScope.activeContacts = "";
$rootScope.activeAbout = "";
$rootScope.activeConnection = "";
$scope.currentPage = 1;
$scope.totalPages = 0;
$scope.loading = true;
$scope.$on('$viewContentLoaded', function () {
$http.get('https://api.spotify.com/v1/search?q=artist:Owl+City+title:On+The+Wing&type=track&limit=1').success(function (data) {
console.log("Success");
})
.error(function (error, status, headers, config) {
console.log(status);
console.log("Error occured");
});
$.getJSON('https://api.spotify.com/v1/search?q=artist:Owl+City+title:On+The+Wing&type=track&limit=1').then(function (data) {
$scope.preview_url = $sce.trustAsResourceUrl(data.tracks.items[0].preview_url);
});
});
}
谢谢你的回答。
对我来说非常好
angular.module('pwilApp',[])
.controller('SongsCtrl', function ($rootScope, $scope,$http) {
var increment = 0;
var laSimilaire = "";
var init = true;
$rootScope.activeHome = "";
$rootScope.activeSongs = "active";
$rootScope.activeAccount = "";
$rootScope.activeContacts = "";
$rootScope.activeAbout = "";
$rootScope.activeConnection = "";
$scope.currentPage = 1;
$scope.totalPages = 0;
$scope.loading = true;
$http.get('https://api.spotify.com/v1/search?q=artist:Owl+City+title:On+The+Wing&type=track&limit=1').success(function (data) {
console.log("Success",data);
$scope.songData = data.tracks.items;
})
.error(function (error, status, headers, config) {
console.log(status);
console.log("Error occured");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="pwilApp" ng-controller="SongsCtrl">
<div ng-repeat="song in songData">
{{song.id}} | {{song.name}}
</div>
相关文章:
- ajax和json错误.php帖子错误
- json错误消息,可以't请参阅SyntaxError
- json错误json.parse:意外的数据结束错误
- 如何处理JSON错误消息
- D3.js 制作树时的 json 错误
- 评估 json 错误
- 使用 Web API MS CRM 时的 JSON 错误
- Javascript/JSON 错误:格式不正确
- JSON 错误:无法读取未定义的属性“xxx”
- 如何序列化全日历源.将循环结构转换为 JSON 错误
- jquery json错误,当我想从Steam市场获取JSON时
- Facebook javascript JSON 错误消息
- 控制台中的JSON错误:意外的令牌:
- 如何摆脱Firefox中的JSON错误
- 使用jQuery$.each,Json错误,结果为1
- 文件不存在错误,package.json错误
- JSON错误:应为..得到'未定义'
- 节点js中的JSON错误未定义为发布数据的前缀
- php后端出现JSON错误
- Jquery Json错误,来自steam的Json,访问控制允许来源