jQuery全局变量值超出函数范围
var hhhh未从displayLocation函数中获取任何值。在控制台中,函数外部为null,而函数内部为获取其值。请提供帮助。谢谢您需要调用函数,然后控制台值: var hhhhh=; 功能显示位置纬度、经度{ var请求=新的XMLHttpRequestjQuery全局变量值超出函数范围,jquery,geolocation,Jquery,Geolocation,var hhhh未从displayLocation函数中获取任何值。在控制台中,函数外部为null,而函数内部为获取其值。请提供帮助。谢谢您需要调用函数,然后控制台值: var hhhhh=; 功能显示位置纬度、经度{ var请求=新的XMLHttpRequest var hhhhhhh = ''; function displayLocation(latitude, longitude) { var request = new XMLHttpReques
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);
您必须在console.log输出之前调用函数 var hhhhh=; 功能显示位置纬度、经度{ //你的代码 hhhhhhh=数值; } 显示位置0,0;
console.loghhhhh;console.loghhhhhh在var hhhhhh=之后执行。您必须调用该函数,然后输出变量的值。感谢您的快速响应,先生。但仍然存在问题。现在,当我调用该函数时,会出现一个错误,即未捕获的引用错误:未定义纬度。是否有解决方案?对不起但我正处于学习阶段。感谢您的快速响应,先生。但仍然存在问题。现在,当我调用函数时,出现了一个错误,即未捕获的参考错误:未定义纬度。有什么解决方案吗?很抱歉,我正处于学习阶段。在调用f时,可能您没有正确传递纬度和经度的值功能
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();
}
displayLocation(lat,long) // you need to call the function and then console the value
console.log(hhhhhhh);