2013-07-07 4 views
0

AngularJS의 JSON HTTP 응답에서 $을 어떻게 파싱합니까? JQuery와없이 YouTube 검색 결과를 잡기 위해 노력

:

$http.get('http://gdata.youtube.com/feeds/api/videos/-/chocolate?alt=json&orderby=published') 
    .success(function (data) { 
     $scope.video_list = data; 
    }) 
    .error(function (data) { 
     $scope.video_list = data; 
}); 

하나에서 오류 응답을 위로하는 때 개체를 얻을; 그 안에서 쿼리 할 수는 없습니다. 여기에 같은

$scope.video_list_fun = function() { 
    $http.get('http://gdata.youtube.com/feeds/api/videos/-/chocolate?alt=json&orderby=published').then(function (response) { 
      $scope.video_list = response.data; 
    }); 
}; 

은 또한 , 체인 else으로 노력했다.

$scope.video_list = (httpGet('http://gdata.youtube.com/feeds/api/videos/-/chocolate?alt=json&orderby=published' 
        )); // HttpGet from http://stackoverflow.com/a/4033310/587021 

이 코드는 작동하지만 예 : JSON.parse; AngularJS는 $과 함께 모든 키를 제거합니다.


내가 their other API와 시도 있습니다 만, 같은 결과를 얻을 수 없습니다 (그것은가 생겼습니다 때문에 게시 된 날짜와 품질에 대한 자신의 유용한 인수를 포함 할 수 없습니다)해야한다.

+1

[이 github 문제] (https://github.com/angular/angular.js/issues/1463)를 확인해보세요. 멋진 해결책 인 것 같지 않습니다 ... –

답변

2

크로스 도메인 제한으로 보입니다. $http.jsonp을 사용하면 효과가 있는지 확인하십시오.

"Using the Data API with JavaScript"alt=json-in-scriptcallback 매개 변수를 사용해야한다고 명시된 YouTube 문서에서 확인하십시오.

그래서 대신에 접근 :

$http.get('http://gdata.youtube.com/feeds/api/videos/-/chocolate?alt=json&orderby=published') 

당신은 액세스 할 :

$http.jsonp('http://gdata.youtube.com/feeds/api/videos/-/chocolate?alt=json-in-script&orderby=published&callback=JSON_CALLBACK') 

주 차이 :

  1. 사용 jsonp 대신 사용하는 대신 alt=jsonalt=json-in-script
  2. get
  3. 는 응답 JSON_CALLBACK를 찾고이 API 문서 만 AngularJS와에 따라 다를 수 있습니다주의 (callback=JSON_CALLBACK 추가 (이 API 문서에 따라 다를 수 있습니다).

Check out this fiddle (작동 코드는 원래 코드를 기준으로 this fiddle과 비교).

JSONP에 대한 자세한 내용은 this wiki 문서를 확인하십시오.

+0

흠, 나는 시도하고있었습니다. 'success' promise 구문을 사용하기 전에'jsonp'; 감사합니다 : D –

+0

두 문법 (똑같은 코드, char에 대한 char)을 시도했지만 둘 다 나에게 효과가없는 것 같습니다. localhost 및 heroku 앱에서 시도했습니다. –

+0

데이터로 무엇을하려고합니까?첫 번째 바이올린 (http://jsfiddle.net/3JWxQ/1/)을 수정하여 일부 비디오 타이틀을 보여 주면서 작동합니까? – Gloopy

관련 문제