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);
の文字列は正しい文字列を生成しているように見えますが、2 番目の文字列は生成されていないため、値がスタックしていないことがわかります。
誰かが助けてくれるなら、前もって感謝します。