<head>
<script type="text/javascript" language="JavaScript">
var obj;
function getData() {
document.body.style.cursor = 'wait';
if (window.XMLHttpRequest) {
obj = new XMLHttpRequest();
} else if (window.ActiveXObject) {
obj = new ActiveXObject("Microsoft.XMLHTTP");
}
var goUrl = "http://news.sohu.com/rss/sports.xml";
obj.onreadystatechange = xmlReady;
obj.open("GET", goUrl, false);
obj.send();
document.body.style.cursor = 'auto';
}
function xmlReady() {
if (obj.readyState == 4) {
if (obj.status == 200) {
var xmlDoc = obj.responseXML;
var titleNodes = xmlDoc.getElementsByTagName("item");
for(var n=0; n<titleNodes.length; n++){
alert(titleNodes[n].childNodes[0].nodeValue);
document.write(titleNodes[n].childNodes[0].nodeValue);
document.write("<br />");
}
}
}
}
</script>
</head>
<body>
晕,我还以为你是要本地的呢
那方案就不一样的,
看看跨域的部分
http://hi.baidu.com/clazy/blog/item/32330c9528199e087bf480fc.html
[color=red]obj.send(null); [/color]
在
var xmlDoc = obj.responseXML;
后执行一下alert(xmlDoc)
先确认是否从服务端取到了内容
再去验证其他的。
我这里网络有限制,
取不到外网的XML内容。
没法试验了 :cry:
你那个是同步请求,应该要这样写
[code="java"]
<head>
<script type="text/javascript" language="JavaScript">
var obj;
function getData() {
document.body.style.cursor = 'wait';
if (window.XMLHttpRequest) {
obj = new XMLHttpRequest();
} else if (window.ActiveXObject) {
obj = new ActiveXObject("Microsoft.XMLHTTP");
}
var goUrl = "http://news.sohu.com/rss/sports.xml";
obj.onreadystatechange = xmlReady;
obj.open("GET", goUrl, false);
obj.send(null);
[color=red] if(obj.onreadystatechange == null)
xmlReady();
[/color]
document.body.style.cursor = 'auto';
}
function xmlReady() {
if (obj.readyState == 4) {
if (obj.status == 200) {
var xmlDoc = obj.responseXML;
var titleNodes = xmlDoc.getElementsByTagName("item");
for(var n=0; n alert(titleNodes[n].childNodes[0].nodeValue);
document.write(titleNodes[n].childNodes[0].nodeValue);
document.write("
");
}
}
}
}
<body>
:cry: 应该是跨域的问题。
把XML的URL换成本地同应用下的,你的代码应该就可以正常使用了。
你也可以这么处理
var goUrl = "http://news.sohu.com/rss/sports.xml";
obj.onreadystatechange = xmlReady;
obj.open("POST", "processAcessRemoteUrl.php?url="+goUrl, false);
然后用 processAcessRemoteUrl.php这个php程序解析参数,去得到远程的url文件,再把内容返回给ajax,
这就是所谓的后台代理解决跨域问题
file_get_content写php函数也不稳定的提问,我不是已经给了解决方式了,