我在玩角定位,用角和一些API服务定位客户端的地址。我得到它来正确定位IP地址,但由于某种原因,我一直无法执行地理查找。API服务运行良好,我的URL似乎也很好。
'use strict';
angular.module('Zeus', [])
.controller('ZeusController', function ($scope, $http) {
function fetchIP() {
$http.get("http://ipv4.myexternalip.com/json")
.then(function (response) {
$scope.ip = response.data.ip;
});
return $scope.ip
}
var ip = String(fetchIP());
function geoIP() {
$http.get("https://freegeoip.net/json/" + String(ip))
.then(function (response) {
$scope.country_code = response.data.country_code;
});
return $scope.country_code;
}
var latLon = geoIP();
});我的Chrome控制台出现了以下错误:
Failed to load resource: the server responded with a status of 404 (OK)
https://freegeoip.net/json/undefinedURL绝对不应该是未定义的,因为使用带有角的IP,url会在web浏览器中返回正确的响应。
发布于 2015-12-27 05:59:50
那是因为geoIP()有回调,你必须处理那个回调
像这样
function geoIP() {
return $http.get("https://freegeoip.net/json/" + String(ip))
.then(function(response) {
return response.data.country_code;
});
}
var latLon;
geoIP().then(function(r) {
latLon = r;
})https://stackoverflow.com/questions/34477975
复制相似问题