【发布时间】:2016-06-30 06:56:21
【问题描述】:
var hhhhhhh = '';
function displayLocation(latitude, longitude) {
var request = new XMLHttpRequest();
var method = 'GET';
var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitude + ',' + longitude + '&sensor=true';
var async = true;
request.open(method, url, async);
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
var data = JSON.parse(request.responseText);
var addressComponents = data.results[0].address_components;
for (i = 3; i < 4; i++) {
hhhhhhh = addressComponents[i].long_name;
}
}
};
request.send();
}
console.log(hhhhhhh);
var hhhhhhh 没有从 displayLocation 函数中获取任何值。在控制台中,函数外部为 null,而函数内部获取其值。请帮忙。谢谢
【问题讨论】:
-
console.log(hhhhhhh)在var hhhhhhh = ''之后立即执行。您必须调用该函数,然后输出变量的值
标签: jquery geolocation