0

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.

2
  • can you post the controller code where you are doing the call? Commented Apr 29, 2016 at 9:34
  • Somewhere along the way your data is being malformed, try using a different browser / computer. The code itself seems fine Commented Apr 29, 2016 at 10:11

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

3 Comments

Still 400 error, don't know why :/ May be a module version or else ?
u can c that it is working in my snippet dont know wats wrong on ur side
I tried on an other computer as you asked. It seems to work, but i now have a problem with CORS(Missing headers Access allow origin) Any guess?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.