这是<javascript基础教程>第7版,第13章的例2.
为什么我一运行,就弹出,"There was a problem with the request 0" 是不是哪里没配好 ??我发现所有的示例ajax程序都会出现此问题?script02.css
img {
border-width: 0;
margin: 5px;
}flickrfeed.xml<feed xmlns="http://www.w3.org/2005/Atom"
      xmlns:dc="http://purl.org/dc/elements/1.1/"  xmlns:flickr="urn:flickr:" xmlns:media="http://search.yahoo.com/mrss/"> <title>Content from Paradise Ridge Sculpture Grove</title>
<link rel="self" href="http://api.flickr.com/services/feeds/photoset.gne?set=72157600976524175&amp;nsid=23922109@N00&amp;lang=en-us" />
<link rel="alternate" type="text/html" href="http://www.flickr.com/photos/dorismith/sets/72157600976524175"/>
<id>tag:flickr.com,2005:http://www.flickr.com/photos/23922109@N00/sets/72157600976524175</id>
<icon>http://farm2.static.flickr.com/1335/882568164_72eee9b41f_s.jpg</icon>
<subtitle>The &lt;a href=&quot;http://www.paradiseridgewinery.com/&quot;&gt;Paradise Ridge Winery&lt;/a&gt; not only has great wines, but they also have a sculpture garden. We visited on 22 July 2007.</subtitle>
<updated>2007-07-24T05:19:08Z</updated>
<generator uri="http://www.flickr.com/">Flickr</generator>
    
<entry>
<title>IMG_0045.JPG</title>
<link rel="alternate" type="text/html" href="http://www.flickr.com/photos/dorismith/882590644/in/set-72157600976524175/"/>
<id>tag:flickr.com,2005:/photo/882590644/in/set-72157600976524175</id>
<published>2007-07-24T05:19:08Z</published>
<updated>2007-07-24T05:19:08Z</updated>
                <dc:date.Taken>2007-07-22T13:42:49-08:00</dc:date.Taken>
<content type="html">&lt;p&gt;&lt;a href=&quot;http://www.flickr.com/people/dorismith/&quot;&gt;Dori Smith&lt;/a&gt; posted a photo:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.flickr.com/photos/dorismith/882590644/&quot; title=&quot;IMG_0045.JPG&quot;&gt;&lt;img src=&quot;http://farm2.static.flickr.com/1063/882590644_5a4a0d89f3_m.jpg&quot; width=&quot;240&quot; height=&quot;180&quot; alt=&quot;IMG_0045.JPG&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</content>
<author>
<name>Dori Smith</name>
<uri>http://www.flickr.com/people/dorismith/</uri>
</author>
        <link rel="enclosure" type="image/jpeg" href="http://farm2.static.flickr.com/1063/882590644_5a4a0d89f3_m.jpg" /> <category term="winery" scheme="http://www.flickr.com/photos/tags/" />
<category term="sonomacounty" scheme="http://www.flickr.com/photos/tags/" />
<category term="sculptures" scheme="http://www.flickr.com/photos/tags/" />
<category term="dorismith" scheme="http://www.flickr.com/photos/tags/" />
<category term="paradiseridge" scheme="http://www.flickr.com/photos/tags/" />
<category term="paradiseridgesculptures" scheme="http://www.flickr.com/photos/tags/" />
                 </entry>
<entry>
<title>IMG_0043.JPG</title>
<link rel="alternate" type="text/html" href="http://www.flickr.com/photos/dorismith/882589162/in/set-72157600976524175/"/>
<id>tag:flickr.com,2005:/photo/882589162/in/set-72157600976524175</id>
<published>2007-07-24T05:19:08Z</published>
<updated>2007-07-24T05:19:08Z</updated>
                <dc:date.Taken>2007-07-22T13:41:55-08:00</dc:date.Taken>
<content type="html">&lt;p&gt;&lt;a href=&quot;http://www.flickr.com/people/dorismith/&quot;&gt;Dori Smith&lt;/a&gt; posted a photo:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.flickr.com/photos/dorismith/882589162/&quot; title=&quot;IMG_0043.JPG&quot;&gt;&lt;img src=&quot;http://farm2.static.flickr.com/1179/882589162_25b4d6bcbe_m.jpg&quot; width=&quot;180&quot; height=&quot;240&quot; alt=&quot;IMG_0043.JPG&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</content>
<author>
<name>Dori Smith</name>
<uri>http://www.flickr.com/people/dorismith/</uri>
</author>
        <link rel="enclosure" type="image/jpeg" href="http://farm2.static.flickr.com/1179/882589162_25b4d6bcbe_m.jpg" /> <category term="winery" scheme="http://www.flickr.com/photos/tags/" />
<category term="sonomacounty" scheme="http://www.flickr.com/photos/tags/" />
<category term="sculptures" scheme="http://www.flickr.com/photos/tags/" />
<category term="dorismith" scheme="http://www.flickr.com/photos/tags/" />
<category term="paradiseridge" scheme="http://www.flickr.com/photos/tags/" />
<category term="paradiseridgesculptures" scheme="http://www.flickr.com/photos/tags/" />
                 </entry></feed>

解决方案 »

  1.   

    script02.js
    window.onload = initAll;
    var xhr = false;function initAll() {
    if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
    }
    else {
    if (window.ActiveXObject) {
    try {
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) { }
    }
    } if (xhr) {
    xhr.onreadystatechange = showPictures;
    xhr.open("GET", "flickrfeed.xml", true);
    xhr.send(null);
    }
    else {
    alert("Sorry, but I couldn't create an XMLHttpRequest");
    }
    }function showPictures() {
    var tempDiv = document.createElement("div");
    var tempText = document.createElement("div");

    if (xhr.readyState == 4) {
    if (xhr.status == 200) {
    var allImages = xhr.responseXML.getElementsByTagName("content"); for (var i=0; i<allImages.length; i++) {
    tempText.innerHTML = allImages[i].textContent;
    tempDiv = tempText.getElementsByTagName("p"); var theText = tempDiv[1].innerHTML;
    theText = theText.replace(/240/g,"75");
    theText = theText.replace(/180/g,"75");
    theText = theText.replace(/_m/g,"_s");
    document.getElementById("pictureBar").innerHTML += theText;
    }
    }
    else {
    alert("There was a problem with the request " + xhr.status);
    }
    }
    }
    script02.html
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>My Second Ajax Script</title>
    <link type="text/css" rel="stylesheet" href="script02.css" />
    <script type="text/javascript" src="script02.js"></script>
    </head>
    <body>
    <div id="pictureBar"> </div>
    </body>
    </html>
      

  2.   

    function showPictures() {
        var tempDiv = document.createElement("div");
        var tempText = document.createElement("div");
                
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                var allImages = xhr.responseXML.getElementsByTagName("content");            for (var i=0; i<allImages.length; i++) {
                    tempText.innerHTML = allImages[i].textContent;
                    tempDiv = tempText.getElementsByTagName("p");                var theText = tempDiv[1].innerHTML;
                    theText = theText.replace(/240/g,"75");
                    theText = theText.replace(/180/g,"75");
                    theText = theText.replace(/_m/g,"_s");
                    document.getElementById("pictureBar").innerHTML += theText;
                }
            }
            else {
                alert("There was a problem with the request " + xhr.status);
            }
        }
    }200 - OK 一切正常,对GET和POST请求的应答文档跟在后面。 
    详见:
    http://www.cnblogs.com/zhubaoxu/archive/2008/03/17/1109315.html看到 alert 提示说明没有找到需要滴文件!调试ajax示例,必须要在本机IIS下配置一个测试站点(虚拟目录),并把html和xml文件放到站点下滴正确位置,
    然后在访问 http://localhost/testSite/script02.html 
    难道书里没有介绍?还是lz没仔细看书?不过俺没看过<javascript基础教程>,对ajax也是一知半解,如有谬误还请大家指正!