我試圖通過使用angularjs $ http .get方法和$ http.JSONP方法獲得「http://maps.google.com/maps/api/geocode/json?address=Canada&sensor=true®ion=USA」的api響應。但它不能正常工作,它什麼都不返回。當我通過REST客戶端和瀏覽器嘗試這個API,它提供了JSON數據,狀態碼200
這裏是我的代碼... 的index.html
<html ng-app="myApp">
<body ng-controller="MyCtrl">
<button ng-click="getlocation()">Get Location</button>
</body>
</html>
app.js
var app = angular.module('myApp',[]);
app.controller('MyCtrl', ['$scope','$http',function ($scope,$http) {
$scope.getlocation=function(){
$scope.method = 'JSONP';
$http({method: $scope.method, url: "http://maps.google.com/maps/api/geocode/json?address=Canada&sensor=true®ion=USA"}).
success(function(data, status) {
$scope.status = status;
$scope.data = data;
alert(JSON.stringify(data));
}).
error(function(data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
alert($scope.data+$scope.status);
});
}
}]);
在瀏覽器中運行此代碼時,它不返回任何內容。引發錯誤。 你能幫我解決這個問題嗎?
提及您遇到的錯誤 – 2015-03-13 06:06:05
狀態碼:404請求失敗響應...您能否告訴我如何調用此api「http://maps.google.com/maps/api/geocode/json?address =加拿大和傳感器=真正的地區=美國「通過angularjs – 2015-03-13 07:22:57