我有一个使用Google Civic Information API来拉动政府代表的功能,当我提交一个硬编码的URL时,它可以按预期工作,但是当我尝试使用PARAMS传递相同的查询时,它仅返回一小部分。两种测试功能的响应都为200。
具有在URL中硬编码的查询参数的功能:
$scope.submit = function() {
$http({
method: 'GET',
url: 'https://www.googleapis.com/civicinfo/v2/representatives?key=my-google-api-key&address=114%20Grand%20St%20Albany%20NY%2012202&fields=officials'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
console.log(response);
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log("it didn't work.");
});
};
控制台记录了26位官员的答复。
具有通过PARAMS传递的查询参数的功能:
$scope.submit = function() {
$http({
method: 'GET',
url: 'https://www.googleapis.com/civicinfo/v2/representatives',
params: {
key: 'my-google-api-key',
address: '114%20Grand%20St%20Albany%20NY%2012202',
fields: 'officials'
}
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
console.log(response);
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log("it didn't work.");
});
};
控制台仅记录8位官员。
我在$ http,$ http.get和$ httpParamSerializer的AngularJS API引用中上下浮动,我一生都无法弄清楚自己可能会丢失什么。我在做什么错了?
答案 0 :(得分:1)
尝试将114 Grand St Albany NY 12202
而不是114%20Grand%20St%20Albany%20NY%2012202
作为地址参数传递。
它应该像这样:
$scope.submit = function() {
$http({
method: 'GET',
url: 'https://www.googleapis.com/civicinfo/v2/representatives',
params: {
key: 'my-google-api-key',
address: '114 Grand St Albany NY 12202',
fields: 'officials'
}
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
console.log(response);
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log("it didn't work.");
});
};
我认为,通过传递114%2520Grand%2520St%2520Albany%2520NY%252012202
作为参数,它将获得两次URL编码,并且您将丢失信息。