gpt4 book ai didi

javascript - 在 AngularJS 中使用 $http.get 检索 JSON 时出现错误 400

转载 作者:行者123 更新时间:2023-12-03 07:03:27 27 4
gpt4 key购买 nike

当我尝试使用 $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 中的值时,它仅显示在 View 中,在一个操作之后,因为我得到了上一首歌曲信息,而不是当前歌曲信息.

这是 Controller 代码:

 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>

关于javascript - 在 AngularJS 中使用 $http.get 检索 JSON 时出现错误 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36934658/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com