2016-04-29 84 views
0

当我尝试从以下URL使用$ http.get检索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");}); 

但问题是,当我尝试在我的$范围更改值用这种方法,这只是显示在视图,之后的一个动作,因为我得到了以前的歌曲信息而不是现在的歌曲。

这里是控制器代码:

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

    }); 

    } 

谢谢您的anwser。

+0

你可以张贴控制器代码,你正在做的电话吗? – rick

+0

您的数据出现故障时,请尝试使用其他浏览器/计算机。代码本身似乎很好 – Und3rTow

回答

1

工作完全正常,我

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>

+0

什么都没发生:/ – durks

+0

还是400错误,不知道为什么:/ 可能是模块版本还是其他? – durks

+0

你可以c,它在我的代码片段中工作不知道Wats在你身边的错误 –

相关问题