简体   繁体   English

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

[英]Error 400 when retrieving JSON with $http.get in AngularJS

I get an error 400 when i try to retrieve the JSON from this URL below with $http.get.当我尝试使用 $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");
    });

I manage to retrieve it with this :我设法用这个检索它:

$.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");});

But the problem is that when i try to change a value in my $scope with this method, it 's only displayed in the view, one action after, because of that i get the previous song info and not the current one.但问题是,当我尝试使用此方法更改 $scope 中的值时,它仅显示在视图中,然后执行一个操作,因为我获得的是上一首歌曲信息,而不是当前歌曲信息。

Here is the controller code :这是控制器代码:

 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);
      });

    });

  }

Thank you for your answer.谢谢你的回答。

Works Perfectly fine for me对我来说非常好

 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM