2010-03-25 20 views
1

我正在學習Ajax,並使用「Sams在10分鐘內自學阿賈克斯」。 我學到了很多,基本理解Ajax。這本書編寫代碼示例,並通過代碼一點一點地解釋每一位的作用。 但是。在這段代碼中有一個由作家錯寫成的東西,我不知道是什麼..Ajax中的RSS閱讀器:幫助代碼,發生了什麼問題/缺少了什麼?

下面是代碼:

<html> 
<head> 
<title>An Ajax RSS Headline Reader</title> 
</head> 
<script language="JavaScript" type="text/javascript"> 
function getXMLHTTPRequest() { 
try { 
req = new XMLHttpRequest(); /* e.g. Firefox */ 
} catch(e) { 
    try { 
    req = new ActiveXObject("Msxml2.XMLHTTP"); 
    /* some versions IE */ 
    } catch (e) { 
    try { 
    req = new ActiveXObject("Microsoft.XMLHTTP"); 
    /* some versions IE */ 
    } catch (E) { 
     req = false; 
    } 
    } 
} 
return req; 
} 
var http = getXMLHTTPRequest(); 

function getRSS() { 
    var myurl = 'rssproxy.php?feed='; 
    var myfeed = document.form1.feed.value; 
    myRand = parseInt(Math.random()*999999999999999); 
    // cache buster 

    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) { 
     // first remove the childnodes 
     // presently in the DM 
     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 = document.getElementById('news') .appendChild(newpara3); 
     newpara3.appendChild(newtext3); 
     newpara3.className = "link"; 
     } 
    } 
    } 
} 
</script> 
<body> 
<center> 
<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" onClick="getRSS()" value="Get Feed"><br><br> 
<div id="news" class="displaybox"> <h4>Feed Titles</h4></div> 
</form> 
</center> 
</html> 

和rssproxy.php看起來是這樣的:

<?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); 
?> 

如果你能幫助我,我將不勝感激!

我只是想知道什麼是錯與此代碼,並沒有得到鏈接到其他的Ajax RSS-教程,除非它是完全一樣的代碼(當然沒有的故障)..

+0

根據本書的TOC,在「簡介」一章中有一節「在線資源和_Errata_」。你檢查過這些資源嗎? – VolkerK 2010-03-25 21:37:55

+0

嗨! 本節內容爲: 「訪問Sams Publishing網站www.samspublishing.com,您可以在此下載示例代碼並獲取勘誤表的更多信息和詳細信息。」 我已經下載了示例代碼,它和我上面寫的完全一樣。我不認爲我的任何設置都是錯誤的,因爲所有其他腳本/代碼都能很好地工作。 感謝您的評論! (: – guzh 2010-03-26 09:18:38

回答

0

我認爲有些事情錯XML內容。可能缺少<鏈接>標記。

+0

我不這麼認爲,因爲它沒有顯示我自己製作的xml文件,要求所有人都使用標籤.. 無論如何感謝!(: – guzh 2010-03-26 08:42:37

1

首先讓我們來測試,如果PHP腳本可以獲取,並在所有

<?php 
error_reporting(E_ALL); ini_set('display_errors', 1); 
// $mysession = curl_init($_GET['feed']); 
$mysession = curl_init('http://www.spiegel.de/schlagzeilen/index.rss'); 


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); 
?> 

提供一個RSS feed剛剛從瀏覽器中調用它,並檢查輸出。
如果可以安裝並使用客戶端腳本調試器。例如。對於Firefox使用Firebug。 Javascript錯誤將顯示在控制檯選項卡中。

+0

哼哼..看起來像捲曲-exstension未啓用.. 我正在使用xampp並已編輯(刪除了對extension = php_curl.dll的註釋)php.ini和php5.ini在php文件夾中,但它仍然無法正常工作..任何建議必須啓用它? – guzh 2010-03-29 10:15:24

+0

'echo'ini:',get_cfg_var('cfg_file_path');'告訴你你要編輯哪個php.ini。可以是apache/bin目錄下的php.ini(取決於版本xampp你正在使用) – VolkerK 2010-03-29 10:37:48