XMLHttpRequest를 사용하여 파일을 자바 스크립트 pgm으로 전송하려고합니다. 내가 관찰 한 것은 XMLHttpRequest.send (null)가 아무런 영향을 미치지 않는 것입니다. 아래 코드에서와 같이 비동기 XMLHttpRequest를 사용하면 스크립트가 완료되고 본문의 텍스트가 표시됩니다. 동기 XMLHttpRequest를 사용하는 경우 XMLHttpRequest.open의 세 번째 인수를 false (기본값은 true)로 설정하면 스크립트가 완료되어 본문에 텍스트가 표시됩니다. 따라서 .send 호출이 아무 효과가없는 것처럼 동작합니다.자바 스크립트 XMLHttpRequest 보내기가 아무런 영향을 미치지 않습니다.
이것은 아마도 파일의 코드 나 위치에 오류가 있음을 의미합니다. 이 코드는 HTML 파일에 있고 test.txt는 같은 디렉토리에 있습니다. Firefox 3.6.13을 사용하여 HTML 파일을 로컬로 엽니 다 (File/Open File ... 메뉴 항목과 함께).
이
내가 경보에 표시되는 내용이다비동기 (점 : readyStatus) : 7 : 1, 8 : 1, 1 : 1, 6, 9 : 1
동기 : 7 : 1, 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>