使用javascript实现Ajax

时间:2015-10-16 15:16:24

标签: javascript html ajax xml

我想实现一个简单的AJAX脚本,它试图显示存储在xml文件中的数据。我尝试了以下代码,但按下按钮后页面看起来是静态的,没有任何响应。该脚本不显示xml内容。

<!DOCTYPE html>
<html>
<head>
  <title>XML</title>
  <script>
    function myfun() {
      if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
      } else {
        req = new ActiveXObject("Microsoft.req");
      }
      req.onreadystatechange = function() {
        if (req.readyState === 4 && req.status === 200) {
          document.getElementById("myid").innerHTML = req.responseText;
        }
      }
      //req.overrideMimeType('text/xml');
      req.open("GET", "myxml.xml", true);
      req.send();
    }
  </script>
</head>
<body>
  <div id="myid">This will change</div>
  <input type="button" value="submit" onclick="myfun()" />
</body>
</html>

XML:

<?xml version="1.0" encoding="UTF-8" ?>
<student>
  <id>100</id>
  <name>tamizh</name>
</student>

0 个答案:

没有答案