2016-11-23 31 views
1


NodeJSを使用してAPIを作成しましたが、APIからAngularJSを使用してデータを取得しようとしていますが、データを出力できません。

ここは私のAPIです。ご覧のとおり、データはJSON形式です。APIを使用してAPIからデータを取得できません

<!DOCTYPE html> 
<html> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
<body> 



<div ng-app="myApp" ng-controller="customersCtrl"> 

<ul> 
    <li ng-repeat="x in myData"> 
    {{ x.artist + ', ' + x.song_title}} 
    </li> 
</ul> 

</div> 

<script> 
var app = angular.module('myApp', []); 
app.controller('customersCtrl', function($scope, $http) { 
    $http.get("https://forgetthis-jafar70.c9users.io").then(function (response) { 
     $scope.myData = response.data.data; 
    }); 
}); 
</script> 

</body> 
</html> 

Iドン:あなたは私がすべてのアーティストの名前とその曲のタイトルを取得するループを作成しようとしています見ることができるようにここに私のAPI

{"status":"success","data":[{"_id":"58175cb6f0c48b2b9821d799","listen_url":"https://www.youtube.com/watch?v=XSbZidsgMfw","image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/4/41/Yonkers_tyler_cover.jpg/220px-Yonkers_tyler_cover.jpg","producers":"Tyler, The Creator","label":"XL Recordings","artist":"Tyler, The Creator","song_title":"Yonkers ","__v":0},{"_id":"581760e7aa7eb70a8cf0d770","listen_url":"https://vimeo.com/179791907","image_url":"https://upload.wikimedia.org/wikipedia/en/2/2a/Frank_Ocean_Blonde_2.jpg","producers":"Frank Ocean & Om'Mas Keith","label":"Boys Don't Cry","artist":"Frank Ocean","song_title":"Nikes","__v":0},{"_id":"582199b38e7a590871b38893","listen_url":"https://www.youtube.com/watch?v=x4pon-hdEXU","image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/f/fa/Domo_Genesis-The_Alchemist-No_Idols.jpg/220px-Domo_Genesis-The_Alchemist-No_Idols.jpg","producers":"Alchemist","label":"Self Released","artist":"Domo Genesis","song_title":"Elimination Chamber (featuring Earl Sweatshirt, Vince Staples & Action Bronson)","__v":0},{"_id":"58219ae58e7a590871b38894","listen_url":"https://soundcloud.com/defjam/frank-ocean-super-rich-kids","image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/f/f5/Super_Rich_Kids.jpg/220px-Super_Rich_Kids.jpg","producers":"Malay","label":"Def Jam","artist":"Frank Ocean","song_title":"Super Rich Kids (featuring Earl Sweatshirt)","__v":0},{"_id":"58219c0a8e7a590871b38895","listen_url":"https://www.youtube.com/watch?v=0FcDXL5Aw0o","image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/d/d0/Hive-earlsweat.jpg/220px-Hive-earlsweat.jpg","producers":"randomblackdude and Matt Martians","label":"Tan Cressida","artist":"Earl Sweatshirt","song_title":"Hive (featuring Casey Veggies and Vince Staples)","__v":0},{"_id":"58219f3f8e7a590871b38896","listen_url":"https://vimeo.com/31781199","image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/2/2e/Frank-Ocean-Thinkin-Bout-You-2012.png/220px-Frank-Ocean-Thinkin-Bout-You-2012.png","producers":"Shea Taylor","label":"Def Jam","artist":"Frank Ocean","song_title":"Thinkin Bout You","__v":0},{"_id":"5821a3e88e7a590871b38897","listen_url":"https://vimeo.com/49091270","image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/7/71/Frankoceanpyramids.jpg/220px-Frankoceanpyramids.jpg","producers":"Frank Ocean, Malay and Om'Mas Keith","label":"Def Jam","artist":"Frank Ocean","song_title":"Pyramids","__v":0},{"_id":"582b4010b5a2eb460b850a2c","listen_url":"https://www.youtube.com/watch?v=FCbWLSZrZfw","image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/8/81/Chum_Earl_Sweatshirt.jpg/220px-Chum_Earl_Sweatshirt.jpg","producers":"randomblackdude, Christian Rich & Hugo (Outro)","label":"Tan Cressida","artist":"Earl Sweatshirt","song_title":"Chum","__v":0}]} 

へのライブリンクは、AngularJSコードです私が間違っていることは分かっていません。

+1

私はあなたが取得するために、CORSを有効にする必要があると思いますデータ。 –

+0

APIまたはAngularJSに問題がありますか?ブラウザでそのURLを取得できますか? jQuery(またはもっと慣れ親しんでいるJavaScript)でGETできますか? –

+4

ここでのエラーは 'XMLHttpRequestはhttps://forgetthis-jafar70.c9users.io/をロードできません。要求されたリソースに 'Access-Control-Allow-Origin'ヘッダーが存在しません。あなたはCORSを研究し、質問を投稿するときにエラーを投稿する必要があるので、他の人があなたのためにエラーを再現する必要はありません。 – Claies

答えて

0

は、あなたのモジュール宣言の後にこれを追加:私はCORSを有効にする必要がありました@Claiesに

app.config(['$sceDelegateProvider', function($sceDelegateProvider) { 
     $sceDelegateProvider.resourceUrlWhitelist(['self', 'https://forgetthis-jafar70.c9users.io/**']); 

}]); 
+0

ありがとうございます。 –

0

感謝を。 index.jsファイル(nodejs)にこれらの数行のコードを追加する必要がありました。

server.use(function(req, res, next) { 
    res.header('Access-Control-Allow-Origin', '*') 
    res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE') 
    res.header('Access-Control-Allow-Headers','Content-Type') 
    next() 
}) 
0

まず、APIをリクエストしているうちに、以下のエラーが発生します。

https://forgetthis-jafar70.c9users.io/ 503(サービス利用不可)

だから、私は、コントローラでの応答をハードコード:

var app = angular.module('myApp', []); 
 
app.controller('customersCtrl', function($scope) { 
 
    $scope.myData = [ 
 
     { 
 
     "_id":"58175cb6f0c48b2b9821d799", 
 
     "listen_url":"https://www.youtube.com/watch?v=XSbZidsgMfw", 
 
     "image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/4/41/Yonkers_tyler_cover.jpg/220px- Yonkers_tyler_cover.jpg", 
 
     "producers":"Tyler, The Creator", 
 
     "label":"XL Recordings", 
 
     "artist":"Tyler, The Creator", 
 
     "song_title":"Yonkers ", 
 
     "__v":0 
 
     }, 
 
     { 
 
     "_id":"581760e7aa7eb70a8cf0d770", 
 
     "listen_url":"https://vimeo.com/179791907", 
 
     "image_url":"https://upload.wikimedia.org/wikipedia/en/2/2a/Frank_Ocean_Blonde_2.jpg", 
 
     "producers":"Frank Ocean & Om'Mas Keith", 
 
     "label":"Boys Don't Cry", 
 
     "artist":"Frank Ocean", 
 
     "song_title":"Nikes", 
 
     "__v":0 
 
     }, 
 
     { 
 
     "_id":"582199b38e7a590871b38893", 
 
     "listen_url":"https://www.youtube.com/watch?v=x4pon-hdEXU", 
 
     "image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/f/fa/Domo_Genesis-The_Alchemist-No_Idols.jpg/220px-Domo_Genesis-The_Alchemist-No_Idols.jpg", 
 
     "producers":"Alchemist", 
 
     "label":"Self Released", 
 
     "artist":"Domo Genesis", 
 
     "song_title":"Elimination Chamber (featuring Earl Sweatshirt, Vince Staples & Action Bronson)", 
 
     "__v":0 
 
     }, 
 
     { 
 
     "_id":"58219ae58e7a590871b38894", 
 
     "listen_url":"https://soundcloud.com/defjam/frank-ocean-super-rich-kids", 
 
     "image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/f/f5/Super_Rich_Kids.jpg/220px-Super_Rich_Kids.jpg", 
 
     "producers":"Malay", 
 
     "label":"Def Jam", 
 
     "artist":"Frank Ocean", 
 
     "song_title":"Super Rich Kids (featuring Earl Sweatshirt)", 
 
     "__v":0 
 
     }, 
 
     { 
 
     "_id":"58219c0a8e7a590871b38895", 
 
     "listen_url":"https://www.youtube.com/watch?v=0FcDXL5Aw0o", 
 
     "image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/d/d0/Hive-earlsweat.jpg/220px-Hive-earlsweat.jpg", 
 
     "producers":"randomblackdude and Matt Martians", 
 
     "label":"Tan Cressida", 
 
     "artist":"Earl Sweatshirt", 
 
     "song_title":"Hive (featuring Casey Veggies and Vince Staples)", 
 
     "__v":0 
 
     }, 
 
     { 
 
     "_id":"58219f3f8e7a590871b38896", 
 
     "listen_url":"https://vimeo.com/31781199", 
 
     "image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/2/2e/Frank-Ocean-Thinkin-Bout-You-2012.png/220px-Frank-Ocean-Thinkin-Bout-You-2012.png", 
 
     "producers":"Shea Taylor", 
 
     "label":"Def Jam", 
 
     "artist":"Frank Ocean", 
 
     "song_title":"Thinkin Bout You", 
 
     "__v":0 
 
     }, 
 
     { 
 
     "_id":"5821a3e88e7a590871b38897", 
 
     "listen_url":"https://vimeo.com/49091270", 
 
     "image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/7/71/Frankoceanpyramids.jpg/220px-Frankoceanpyramids.jpg", 
 
     "producers":"Frank Ocean, Malay and Om'Mas Keith", 
 
     "label":"Def Jam", 
 
     "artist":"Frank Ocean", 
 
     "song_title":"Pyramids", 
 
     "__v":0 
 
     }, 
 
     { 
 
     "_id":"582b4010b5a2eb460b850a2c", 
 
     "listen_url":"https://www.youtube.com/watch?v=FCbWLSZrZfw", 
 
     "image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/8/81/Chum_Earl_Sweatshirt.jpg/220px-Chum_Earl_Sweatshirt.jpg", 
 
     "producers":"randomblackdude, Christian Rich & Hugo (Outro)", 
 
     "label":"Tan Cressida", 
 
     "artist":"Earl Sweatshirt", 
 
     "song_title":"Chum", 
 
     "__v":0 
 
     } 
 
    ]; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="customersCtrl"> 
 
<ul> 
 
    <li ng-repeat="x in myData"> 
 
    {{ x.artist + ', ' + x.song_title}} 
 
    </li> 
 
</ul> 
 
</div>

関連する問題