火狐3.5以上

<!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>孟宪会之AJAX跨域请求测试</title>
 </head>
<body>
  <input type='button' value='开始测试' onclick='crossDomainRequest()' />
  <div ></div>

  <script type="text/javascript">
    //<![CDATA[
    var xhr = new XMLHttpRequest();
    var url = 'http://192.168.0.94:8080/now.php';
    function crossDomainRequest() {
      document.getElementById("content").innerHTML = "开始……";
      if (xhr) {
        xhr.open('GET', url, true);
        xhr.onreadystatechange = handler;
        xhr.send();
      } else {
        document.getElementById("content").innerHTML = "不能创建 XMLHttpRequest";
      }
    }
    function handler(evtXHR) {
      if (xhr.readyState == 4) {
        if (xhr.status == 200) {
          var response = xhr.responseText;
          document.getElementById("content").innerHTML = "结果:" + response;
        } else {
          document.getElementById("content").innerHTML = "不允许跨域请求。";
        }
      }
      else {
        document.getElementById("content").innerHTML += "<br/>执行状态 readyState:" + xhr.readyState;
      }
    }
    //]]>
  </script>

</body>
</html>

下面是IE8上以的

<!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>孟宪会之AJAX跨域请求测试</title>  
</head>  
<body>  
  <input type='button' value='开始测试' onclick='crossDomainRequest()' />  
  <div + xhr.responseText;   
    }   
  </script>
</body>  
</html>

 

相关文章: