XML问题 - 加载空白页面

时间:2012-07-04 21:27:03

标签: javascript xml ajax

我是XML编码新手,我现在正在网站上工作,以帮助我提高技能。我认为我得到了该页面的所有语法,但它似乎没有工作。这是索引页面的代码以及我正在尝试使用的xml文件。

<html>
<body>

<script type="text/javascript">
var link= "http://api.remix.bestbuy.com/v1/stores(area(55423,10))+products(name=playstation*)?show=storeId,name,products.sku,products.name&page=1&apiKey=ydpyq9h9cmpmzaakbawv9mzk";
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","test.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 
document.write("<table border='1'>");
var x = xmlDoc.getElementsByTagName("name");
for (i=0;i<5;i++)
  { 
  document.write("<tr><td>"); 
  document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
   document.write("Step 2");
  document.write("</td><td>");
  document.write(x[i].getElementsByTagName("distance")[0].childNodes[0].nodeValue);
  document.write("</td></tr>");
  }
document.write("</table>");
</script>

</body>
</html>

和XML我得到了最佳购买请求样本

<stores warnings="Distances are in terms of miles (default). Your product criteria matches too many records. That exceeds number of records that we allow on the product side of a store-product query. We've automatically truncated the products down to the first 100. These results are not complete. Avoid this by narrowing the number of products in your query." currentPage="1" totalPages="1" from="1" to="10" total="10" queryTime="0.015" totalTime="0.153" canonicalUrl="/v1/stores(area("55423",10))+products(name="playstation*")?show=storeId,name,products.sku,products.name&apiKey=ydpyq9h9cmpmzaakbawv9mzk" partial="false">
  <store>
    <storeId>281</storeId>
    <name>Richfield</name>
    <products>
      <product>
        <sku>9984133</sku>
        <name>Assassin's Creed Brotherhood - PlayStation 3</name>
      </product>
      <product>
        <sku>7917141</sku>
        <name>Assassin's Creed Greatest Hits - PlayStation 3</name>
      </product>
      <product>
        <sku>2613621</sku>
        <name>Assassin's Creed: Revelations - PlayStation 3</name>
      </product>
      <product>
        <sku>3109269</sku>
        <name>Assassin's Creed: Revelations Collector's Edition (Game Guide) - Xbox 360, PlayStation 3, Windows</name>
      </product>
      <product>
        <sku>3109065</sku>
        <name>Assassin's Creed: Revelations The Complete Official Guide (Game Guide) - Xbox 360, PlayStation 3, Windows</name>
      </product>
      <product>
        <sku>9927337</sku>
        <name>Batman: Arkham Asylum Game of the Year Edition - PlayStation 3</name>
      </product>
      <product>
        <sku>3116162</sku>
        <name>Batman: Arkham City (Signature Series Game Guide) - Windows, PlayStation 3, Xbox 360</name>
      </product>
      <product>
        <sku>5260722</sku>
        <name>Batman: Arkham City - Game of the Year Edition - PlayStation 3</name>
      </product>
      <product>
        <sku>2173056</sku>
        <name>Batman: Arkham City - PlayStation 3</name>
      </product>
      <product>
        <sku>3550073</sku>
        <name>Battlefield 3 (Game Guide) - Xbox 360, PlayStation 3, Windows</name>
      </product>
</products>
  </store>
</stores>

1 个答案:

答案 0 :(得分:1)

您必须将数据加载后执行的代码放在onreadystatechange对象的xmlhttp事件中:http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

xmlhttp.onreadystatechange = function() {
    if (httprequest.readyState == 4) {
        // Response finished
        if (httprequest.status = 200) {
           // everything OK
           // Parse the data
        } else {
           // Something gone wrong with the request
        }
    }
}

修改
现在看看你的代码,我意识到你正在使用var x = xmlDoc.getElementsByTagName("name");。这将选择每个标记name,包括内部标记。尝试使用store获取每个名称而不是名称,然后查找product

此外,您在for循环中使用了硬编码限制。请改为使用x.lengthfor (var i=0; i<x.length;i++)