XMLHttpRequest 不工作
我使用以下代码每 2 秒动态更新 HTML 中的 TextArea 元素,而无需重新加载页面。但是,alert(request.readystate)
返回未定义而不是数字 1-4。当我尝试 if(request)alert("Request object")
时,它会按预期提醒用户“Request object”。我不知道为什么这不起作用,我已经尝试了几个小时来解决这个问题!
我的代码:
<script type="text/javascript">
function init(){
var url="http://www.suchandsuch.net/ChatBowl/text.txt";
var request= new XMLHttpRequest();
request.open("POST",url,true);
alert(request.readystate);
request.onload= function(){
if (request.readystate ==4 && request.status == 200){
document.getElementById('textarea').innerHTML=request.responseText;
}
}
request.send(null);
}
var int=self. setInterval('init()', 2000);
</script>
我感谢任何帮助。
The following code I am using to dynamically update the TextArea element in my HTML every 2 seconds without reloading the page. However, the alert(request.readystate)
is returning undefined instead of a number 1-4. When I tried if(request) alert("Request object")
it alerts the user "Request object" as expected. I have no clue why this isn't working and I have been trying to figure this out for hours!
My code:
<script type="text/javascript">
function init(){
var url="http://www.suchandsuch.net/ChatBowl/text.txt";
var request= new XMLHttpRequest();
request.open("POST",url,true);
alert(request.readystate);
request.onload= function(){
if (request.readystate ==4 && request.status == 200){
document.getElementById('textarea').innerHTML=request.responseText;
}
}
request.send(null);
}
var int=self. setInterval('init()', 2000);
</script>
I appreciated any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JavaScript 区分大小写,并且您输入的 ReadyState 错误:
readyState 不是 readystate
MSDN docs
并且回调函数应该分配给
onreadystatechange
:request.send();
JavaScript is case sensitive and you typed the readyState wrong:
readyState not readystate
MSDN docs
And the callback function should be assigned to
onreadystatechange
:request.send();
也许而不是
尝试
maybe instead of
try