-3
我使用Ajax在我的網站中運行PHP腳本。我想爲來自PHP腳本的響應設置一個JS變量。JavaScript responseText沒有被保存爲可變?
這個腳本應該使變量 「STOPAT」 設置爲42
這是我的錯誤:
這裏是我的代碼:
function reqListener() {
console.log(this.responseText);
}
var oReq = new XMLHttpRequest(); //New request object
oReq.onload = function() {
var stopAt = (this.responseText)
};
oReq.open("get", "gennum.php", true);
oReq.send();
theWheel.animation.stopAngle = stopAt;
這裏是gennum.php:
<?php
echo json_encode(42);
?>
謝謝!希望你們能解決我的問題! :)
有沒有什麼辦法可以通過使用全局變量來做到這一點...或什麼? – Jake
您仍然需要在函數內設置「stopAngle」。 ['XMLHttpRequest.send'](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send)是異步的,所以它會盡快返回而不用等待請求完成,並且該變量仍然不會在'send()'調用下定義。 –
@jake .......... –