admin管理员组文章数量:1022705
I wrote the following simple javascript code
<html xmlns="" xml:lang="en" lang="en">
<head> <title>ajax</title> </head>
<body>
<script type="text/javascript">
var xhr = new XMLHttpRequest();
var url = "http://localhost/javascript/test.php";
xhr.open("GET", url);
alert(xhr);
xhr.send(null);
xhr.onreadystatechange = function () {alert("change");}
alert(xhr.responseText);
</script>
</body>
</html>
the result shows xhr.responseText is empty. But following javascript code works fine. Why?
<html xmlns="" xml:lang="en" lang="en">
<head><title>ajax</title></head>
<body>
<script type="text/javascript">
var xhr = new XMLHttpRequest();
var url = "http://localhost/javascript/test.php";
xhr.open("GET", url);
xhr.send(null);
xhr.onreadystatechange =
function () {
if (xhr.readyState == 4) alert(xhr.responseText);
};
</script>
</body>
</html>
Following is the simple PHP code: test.php
<?php
echo date("F j, Y, H:i:s");
I wrote the following simple javascript code
<html xmlns="http://www.w3/1999/xhtml" xml:lang="en" lang="en">
<head> <title>ajax</title> </head>
<body>
<script type="text/javascript">
var xhr = new XMLHttpRequest();
var url = "http://localhost/javascript/test.php";
xhr.open("GET", url);
alert(xhr);
xhr.send(null);
xhr.onreadystatechange = function () {alert("change");}
alert(xhr.responseText);
</script>
</body>
</html>
the result shows xhr.responseText is empty. But following javascript code works fine. Why?
<html xmlns="http://www.w3/1999/xhtml" xml:lang="en" lang="en">
<head><title>ajax</title></head>
<body>
<script type="text/javascript">
var xhr = new XMLHttpRequest();
var url = "http://localhost/javascript/test.php";
xhr.open("GET", url);
xhr.send(null);
xhr.onreadystatechange =
function () {
if (xhr.readyState == 4) alert(xhr.responseText);
};
</script>
</body>
</html>
Following is the simple PHP code: test.php
<?php
echo date("F j, Y, H:i:s");
Share
Improve this question
asked Mar 7, 2011 at 8:23
PeopleMoutainPeopleSeaPeopleMoutainPeopleSea
1,5142 gold badges15 silver badges26 bronze badges
2 Answers
Reset to default 5When an answer es from the server, the browser will fire the object's onreadystatechanged event, causing the function you attached to it, to be fired, what you need to do in your first example is put this line :
alert(xhr.responseText);
inside the function you attach to the onreadystatechanged event.
otherwise the browser will just process the line immediately after sending the request.
In addition, you must check that the readystate is 4 (4: request finished and response is ready), and its also worth it to check that the status is 200, which means that everything's ok.
Similar to r0nny's answer, your first example gets processed before your ajax called has been returned. Using the onreadystatechange
waits for the ajax response, then does what you need it to.
I wrote the following simple javascript code
<html xmlns="" xml:lang="en" lang="en">
<head> <title>ajax</title> </head>
<body>
<script type="text/javascript">
var xhr = new XMLHttpRequest();
var url = "http://localhost/javascript/test.php";
xhr.open("GET", url);
alert(xhr);
xhr.send(null);
xhr.onreadystatechange = function () {alert("change");}
alert(xhr.responseText);
</script>
</body>
</html>
the result shows xhr.responseText is empty. But following javascript code works fine. Why?
<html xmlns="" xml:lang="en" lang="en">
<head><title>ajax</title></head>
<body>
<script type="text/javascript">
var xhr = new XMLHttpRequest();
var url = "http://localhost/javascript/test.php";
xhr.open("GET", url);
xhr.send(null);
xhr.onreadystatechange =
function () {
if (xhr.readyState == 4) alert(xhr.responseText);
};
</script>
</body>
</html>
Following is the simple PHP code: test.php
<?php
echo date("F j, Y, H:i:s");
I wrote the following simple javascript code
<html xmlns="http://www.w3/1999/xhtml" xml:lang="en" lang="en">
<head> <title>ajax</title> </head>
<body>
<script type="text/javascript">
var xhr = new XMLHttpRequest();
var url = "http://localhost/javascript/test.php";
xhr.open("GET", url);
alert(xhr);
xhr.send(null);
xhr.onreadystatechange = function () {alert("change");}
alert(xhr.responseText);
</script>
</body>
</html>
the result shows xhr.responseText is empty. But following javascript code works fine. Why?
<html xmlns="http://www.w3/1999/xhtml" xml:lang="en" lang="en">
<head><title>ajax</title></head>
<body>
<script type="text/javascript">
var xhr = new XMLHttpRequest();
var url = "http://localhost/javascript/test.php";
xhr.open("GET", url);
xhr.send(null);
xhr.onreadystatechange =
function () {
if (xhr.readyState == 4) alert(xhr.responseText);
};
</script>
</body>
</html>
Following is the simple PHP code: test.php
<?php
echo date("F j, Y, H:i:s");
Share
Improve this question
asked Mar 7, 2011 at 8:23
PeopleMoutainPeopleSeaPeopleMoutainPeopleSea
1,5142 gold badges15 silver badges26 bronze badges
2 Answers
Reset to default 5When an answer es from the server, the browser will fire the object's onreadystatechanged event, causing the function you attached to it, to be fired, what you need to do in your first example is put this line :
alert(xhr.responseText);
inside the function you attach to the onreadystatechanged event.
otherwise the browser will just process the line immediately after sending the request.
In addition, you must check that the readystate is 4 (4: request finished and response is ready), and its also worth it to check that the status is 200, which means that everything's ok.
Similar to r0nny's answer, your first example gets processed before your ajax called has been returned. Using the onreadystatechange
waits for the ajax response, then does what you need it to.
本文标签: javascriptWhen will XmlHttpResponseresponseText be filled with reponse from the serverStack Overflow
版权声明:本文标题:javascript - When will XmlHttpResponse.responseText be filled with reponse from the server? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745559811a2156094.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论