当我使用angularJS调用Youtube api时,我遇到了JSON响应问题:
代码:
sc.factory('videoApi', ['$resource', function($resource) {
return $resource( 'https://www.googleapis.com/youtube/v3/search',
{callback: 'JSON_CALLBACK'},
{
get: {
method: 'GET',
params: {
order: 'date',
part: 'snippet',
channelId: 'UCY8ZYY7YSQ80JW1Ijo2BJvA',
type: 'video',
key: '*** my key ***' },
isArray : false,
}
} );
}]);
sc.controller('scontroller', ['$scope','videoApi', function ($scope,videoApi) {
videoApi.get().$promise.then(function(data) {
console.log(data);
// console.log(data);
//for (i = 0; i < data.length; i++) {
//var question = editQuestion(data[i].title, data[i].time, data[i].voteyes, data[i].voteno, true, data[i].num, data[i].ip);
//questionList.push(question);
//}
});
$scope.inputTest = "test";
}]);
回应:
1: "c"102: "h"103: "Q"104: "_"105: "m"106: "z"107: "W"108: "J"109: "s"110: "/"111: "1"112: "E"113: "G"114: "6"115: "t"116: "T"117: "T"118: "v"119: "F"120: "U"121: "g"122: "S"123: "L"124: "y"125: "j"126: "S"127: "7"128: "G"129: "M"130: "H"131: "U"132: "Z"133: "x"134: "G"135: "R"136: "5"137: "M"138: "\"139: """140: """141: ","142: "↵"143: " "144: """145: "n"146: "e"147: "x"148: "t"149: "P"150: "a"151: "g"152: "e"153: "T"154: "o"155: "k"156: "e"157: "n"158: """159: ":"160: " "161: """162: "C"163: "A"164: "U"165: "Q"166: "A"167: "A"168: """169: ","170: "↵"171: " "172: """173: "p"174: "a"175: "g"176: "e"177: "I"178: "n"179: "f"180: "o"181: """182: ":"183: " "184: "{"185: "↵"186: " "187: " "188: """189: "t"190: "o"191: "t"192: "a"193: "l"194: "R"195: "e"196: "s"197: "u"198: "l"199: "t"200: "s"201: """202: ":"203: " "204: "1"205: "1"206: ","207: "↵"208: " "209: " "210: """211: "r"212: "e"213: "
然而,当我在chrome中查看网络选项卡时,我得到了正确的响应格式:
// API callback JSON_CALLBACK({"kind": "youtube#searchListResponse","etag": "\"NO6QTeg0-3ShswIeqLchQ_ ....
您知道为什么我会在数据变量中获得此响应格式吗?
提前致谢,祝你有个美好的一天:)
答案 0 :(得分:0)
这将解决您的问题:
return $resource( 'https://www.googleapis.com/youtube/v3/search',
{callback: 'JSON_CALLBACK'},
{
get: {
method: 'JSONP',
params: {
order: 'date',
part: 'snippet',
channelId: 'UCY8ZYY7YSQ80JW1Ijo2BJvA',
type: 'video',
key: 'AIzaSyBrmLZjy9P_HfGslKK6VzwKRhP3eVUqLVk' },
isArray : false,
}
} );
jsfiddle链接:http://jsfiddle.net/nrg0hnx1/