durks
durks

Reputation: 1

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('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.

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.

Upvotes: 0

Views: 263

Answers (1)

Mohsin Muzawar
Mohsin Muzawar

Reputation: 1212

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>

Upvotes: 1

Related Questions