新闻阅读器,responseXML为空
照着书上打得,老是出错,http.responseXML is null,请各路英雄帮忙看看问题出哪
<html><head><script language="javascript" src="rssd.js"></script></head><style type="text/css">.title{ font:16px bold helvetica,arial,sans-serif; padding:0px 30px 0px 30px; text-decoration:underline;}</style><body><h3>An Ajax RSS Reader</h3><form name="form1">URL of Rss feed:<input type="text" name="feed" size="50" value="http://"><input type="button" value="Get Feed" onclick="getRSS()"><br><div id="news"><h4>Feed Titles</h4></div></form></body></html>
function getXMLHTTPRequest(){ var request=false; try{ request=new XMLHttpRequest(); } catch(err1) { try{ request=new ActiveXObject("Msxml2.XMLHTTP"); } catch(err2) { try{ request=new ActiveXObject("Microsoft.XMLHTTP"); } catch(err3) { request=false; } } } return request;}var http=getXMLHTTPRequest();function getRSS(){ var myurl='rssproxy.php?feed='; var myfeed=document.form1.feed.value; myRand=parseInt(Math.random()*999999999); var modurl=myurl+escape(myfeed)+"&rand="+myRand; http.open("GET",modurl,true); http.onreadystatechange=useHttpResponse; http.send(null);}function useHttpResponse(){ if(http.readyState==4){ if(http.status==200){/* alert("The server said:"+http.responseText);}else{ alert("An error has occurred:"+http.statusText);}}}*/ while(document.getElementById('news').hasChildNodes()) { document.getElementById('news').removeChild(document.getElementById('news').firstChild); } var titleNodes=http.responseXML.getElementsByTagName("title"); var descriptionNodes=http.responseXML.getElementsByTagName("description"); var linkNodes=http.responseXML.getElementsByTagName("link"); for(var i=1;i<titleNodes.length;i++) { var newtext=document.createTextNode(titleNodes[i].childNodes[0].nodeValue); var newpara=document.createElement('p'); var para=document.getElementById('news').appendChild(newpara); newpara.appendChild(newtext); newpara.className="title"; var newtext2=document.createTextNode(descriptionNodes[i].childNodes[0].nodeValue); var newpara2=document.createElement('p'); var para2=document.getElementById('news').appendChild(newpara2); newpara2.appendChild(newtext2); newpara2.className="descrip"; var newtext3=document.createTextNode(linkNodes[i].childNodes[0].nodeValue); var newpara3=document.createElement('p'); var para3=docuement.getElementById('news').appendChild(newpara3); newpara3.appendChild(newtext3); newpara3.className="link"; } } }}
<?php$mysession=curl_init($_GET['feed']);curl_setopt($mysession,CURLOPT_HEADER,false);curl_setopt($mysession,CURLOPT_RETURNTRANSFER,true);$out=curl_exec($mysession);header('Content-Type:text/xml');echo $out;curl_close($mysession);?>
如果代码没有跨域,则返回的是正确的xml的格式吗
[解决办法]
如果跨域了的话,还是让服务器代理一下任务、在SERVER端获取远程数据吧。当然,这需要服务器的支持,可以在PHP.INI里设置一下。
<?php $url='http://www.baidu.com/'; $html=file_get_contents($url); //print_r($http_response_header); //.........?>
[解决办法]