我在解析通过调用URL获得的响应方面遇到了麻烦:
$scope.getSuggestions = function (val) {
return $http.jsonp('https://Suggestions-api.net/v1/suggest?query=' + val + '&callback=JSON_CALLBACK').success(function (data) {
return data.value;
});
};
$scope.JSON_CALLBACK = function (data) {
return data.value;
};
编辑:用户界面:
<input type="text" id="search" ng-model="searchText" typeahead="JumboID for JumboID in getSuggestions($viewValue)" />
我得到的回复格式为:
{"@odata.context":"https://Suggestions-api.net/indexes('jumboindex')/$metadata#docs(JumboID)",
"value":
[{"@search.text":"90911491","JumboID":"4454b90146a98733529b38942d028acb:f011f34214422e4903f4590819f41c21"},
{"@search.text":"9094993","JumboID":"07c927eae265db736a20650e77d2e945:f011f34214422e4903f4590819f41c21"},
{"@search.text":"90910444","JumboID":"0155444a12dfadff2451e06be40a98d2:f011f34214422e4903f4590819f41c21"},
{"@search.text":"9090 Whiskey Bottom Road Laurel Maryland 20723","JumboID":"34b239bd2dba98df6f5ae26a1f66c2fb:80880b9b1d7261f24c4e1c341853ec4e"},
{"@search.text":"9090 Alta Drive Las Vegas Nevada 89145","JumboID":"d0746b298cfca4c5df3699823e32f6c3:80880b9b1d7261f24c4e1c341853ec4e"}]}
它在控制台中抛出错误:
Uncaught SyntaxError: Unexpected token :
请帮帮我。
答案 0 :(得分:0)
在网址链接周围加上引号
答案 1 :(得分:0)
请注意angularjs documentation for jsonp说:
回调的名称应该是字符串JSON_CALLBACK。
为了使它能够工作,你需要将回调名称传递给服务器并在后端的函数调用中包装json(有关详细信息,请参阅this SO,您将得到这个想法)。
基本上你的网址应该是:
https://Suggestions-api.net/v1/suggest?query=ABCD&callback=JSON_CALLBACK
然后服务器响应应该如下:
JSON_CALLBACK(
{"@odata.context":"https://Suggestions-api.net/indexes('jumboindex')/$metadata#docs(JumboID)",
"value": [
{"@search.text":"90911491","JumboID":"4454b90146a98733529b38942d028acb:f011f34214422e4903f4590819f41c21"},
{"@search.text":"9094993","JumboID":"07c927eae265db736a20650e77d2e945:f011f34214422e4903f4590819f41c21"},
{"@search.text":"90910444","JumboID":"0155444a12dfadff2451e06be40a98d2:f011f34214422e4903f4590819f41c21"},
{"@search.text":"9090 Whiskey Bottom Road Laurel Maryland 20723","JumboID":"34b239bd2dba98df6f5ae26a1f66c2fb:80880b9b1d7261f24c4e1c341853ec4e"},
{"@search.text":"9090 Alta Drive Las Vegas Nevada 89145","JumboID":"d0746b298cfca4c5df3699823e32f6c3:80880b9b1d7261f24c4e1c341853ec4e"}
]}
)