私は少し立ち往生しています.fillを呼び出してJSON.parseで結果を返す2つの関数があります. しかし、結果を console.log にすると、「未定義」になります。
これは、リクエストを処理する私の関数です:
function caller(url,cfunc){
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
function call_data(url,data){
caller(url,function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
return( JSON.parse (xmlhttp.responseText) );
}
});
}
呼び出しは次のとおりです。
result = call_data('./chk_login.php',0);
console.log(result);
Chrome imによると、xhrリクエストは完全に問題なく取得され、出力が表示されます。しかし、console.log には undefined と表示されます... これが私の PHP スクリプトでもあることがわかります。
<?
$var = 1;
echo json_encode($var);
exit;
?>
問題の原因は何ですか?
あなたが助けてくれることを願っています! ありがとう!