ajax GET xmlHTTPrequest 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17749681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GET xmlHTTPrequest not working
提问by user2166538
i am trying to make an ajax commenting system where if a new comment is posted, the document title changes to (1) website title (like twitter)
我正在尝试制作一个 ajax 评论系统,如果发布新评论,文档标题将更改为 (1) 网站标题(如 Twitter)
my code is here
我的代码在这里
The xmlHTTPrequest
xmlHTTP 请求
function loadXMLDoc7(url)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",url,false);
xmlhttp.send(null);
document.getElementById('newcomments').innerHTML=xmlhttp.responseText;
}
The PHP
PHP
echo "<script type='text/javascript'>
function auto2comments()
{
var MyDiv1 = document.getElementById('uiuiui');";
echo "loadXMLDoc7(MyDiv1.innerHTML)";
echo "}";
echo "setInterval(\"auto2comments()\",15000);</script>";
}
The DIV uiuiui contains /newcommentingi.php?show=0&id=username
The problem is when the Newcomments DIV gets filled, it shows
ID =
Show = 0
why?
DIV uiuiui 包含 /newcommentingi.php?show=0&id=username 问题是当 Newcomments DIV 被填满时,它显示
ID =
Show = 0
为什么?
采纳答案by Dr Rob Lang
The XmlHttpRequest object is asynchronous, which means that when it has the data ready, it returns it in a method. It is best to create a function to act as an event handler so that when the server responds, it calls the event handler function.
XmlHttpRequest 对象是异步的,这意味着当它准备好数据时,它会在一个方法中返回它。最好创建一个函数来充当事件处理程序,以便在服务器响应时调用事件处理程序函数。
I think the solution you need is similar to here: How to get the response of XMLHttpRequest?
我认为您需要的解决方案类似于此处:How to get the response of XMLHttpRequest?

