XMLHttpRequestを使用して、ファイルをJavascriptのpgmに転送しようとしています。私が観察しているのは、XMLHttpRequest.send(null)は何の効果もないようです。下のコードのように非同期XMLHttpRequestを使用すると、スクリプトが完了し、本文のテキストが表示されます。同期XMLHttpRequestを使用する場合、XMLHttpRequest.openの3番目の引数をfalse(デフォルトはtrue)に設定すると、スクリプトも完了し、本文がテキストを表示します。つまり、.send呼び出しが効果がないかのように動作します。Javascript XMLHttpRequest sendは効果がありません
これはおそらく、ファイルのコードまたは場所にエラーがあることを意味します。コードはHTMLファイルにあり、test.txtは同じディレクトリにあります。 Firefox 3.6.13を使用してHTMLファイルをローカルに開きます(ファイル/ファイルを開く...メニュー項目を使用しています)。
この
はIアラートに見るものである。非同期(点:readyStatus):7:0,8:0,1:1、6、9:1
同期:7:0,8:0 、9:1
私は何が間違っているのか、それをさらにデバッグする方法については何か提案をいただきたいと思います。
<html>
<head>
<script type="text/javascript">
function test(data)
{
alert(data);
}
function handlerQ()
{
alert("point 1 readyState="+this.readyState);
if(this.readyState == 4 && this.status == 200)
{
// so far so good
alert("point 2");
if(this.responseXML != null && this.responseXML.getElementById('test').firstChild.data)
// success!
{
alert("point 3");
test(this.responseXML.getElementById('test').firstChild.data);
}
else
{
alert("point 4");
test(null);
}
}
else if (this.readyState == 4 && this.status != 200)
{
// fetched the wrong page or network error...
alert("point 5");
test(null);
}
alert("point 6");
}
var clientQ = new XMLHttpRequest();
alert("point 7 readyState="+clientQ.readyState);
clientQ.onreadystatechange = handlerQ;
alert("point 8 readyState="+clientQ.readyState);
clientQ.open("GET", "test.txt",false); // asynchronous
alert("point 9 readyState="+clientQ.readyState);
clientQ.send(null);
alert("point 10 readyState="+clientQ.readyState);
</script>
</head>
<body>
This is the body
</body>
</html>