我正在尝试编写一个脚本来检索 WOEID 并与 Yahoo Weather API 交互。我正在根据我正在使用的数据库中的东西的纬度和经度值构建一个 URL,并且可以完美地做到这一点。
但是,当将该 URL 存储为可以在其他函数中使用的字符串时,我遇到了麻烦。经过一些初步阅读后,它似乎与 onreadystatechange 和范围有关,但我似乎无法理解它来存储我的变量。
到目前为止,这是我的代码:
//<![CDATA[
var latitude = "";
var longitude = "";
var yahooAppID = "";
var yql = "";
//example yahoo request
//http://where.yahooapis.com/geocode?q=38.898717,+-77.035974&gflags=R&appid=SKUTk24k
function getLatLng() {
var routeID = 5;
var get = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
get.open('POST','process.php', true)
get.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
get.send('command=fetch&rid='+routeID);
get.onreadystatechange = function(){
if(get.readyState==4 && get.status == 200) {
os = eval('(' + get.responseText + ')');
latitude = os.start.lat;
longitude = os.start.lng;
//var yql = 'select * from flickr.places where lat='+latitude+' and lon='+longitude;
yql = "select * from flickr.places where lat=" +latitude+ " and lon="+longitude;
}
document.write(yql);
}
document.write(yql);
}
function test() {
getLatLng();
}
//]]>
第一个document.write(yql);
似乎产生了正确的字符串,但第二个没有,所以我知道该值没有卡住。
如果有人可以提供帮助,请提前致谢。