0
PHPファイルからデータを取得する関数を作成しました。私はそれを解析し、警告ボックスを使って表示するとうまくいきますが、値を返そうとすると、定義されていません。この出来事nullを返すJavascript関数
function getUser() {
var httpRequest = new createAjaxRequestObject();
httpRequest.open('GET', 'getUser.php', true);
var name;
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
name = JSON.parse(httpRequest.responseText);
alert(name); //this works just fine and display the name john
} else {
alert('Problem with request');
}
}
}
httpRequest.send();
return name; //however this is returning null
}
Ajaxは非同期です - あなたは名前の変数が、Ajaxリクエストを返していますそれを設定することはまだ完了していません。 –