当前位置: 代码迷 >> Ajax >> XMLHttpRequest.open('post', url, true)拒绝访问有关问题
  详细解决方案

XMLHttpRequest.open('post', url, true)拒绝访问有关问题

热度:810   发布时间:2012-05-12 15:39:31.0
XMLHttpRequest.open('post', url, true)拒绝访问问题
源代码
HTML code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>Simple Non-Server AJAX Example</TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <script>
  xhr = null;
  function updateCharacters() {
    try
    {
        if(window.XMLHttpRequest) {
            //Non-IE.
            xhr = new XMLHttpRequest();
            alert("Non-IE");
        }
        else {
            //IE
            xhr = ActiveXObject("Microsoft.XMLHTTP");
            alert("IE");
        }
        xhr.onreadystatechange = callbackHandler;
        url = document.getElementById("selShow").value+".htm";
        alert(url);
        xhr.open('post', url, true);
        xhr.send(null);
    }
    catch (e)
    {
        alert("Error: "+e.description);
    }
  }

  function callbackHandler() {
    alert("cb");
    if(xhr.readyState == 4) {
        document.getElementById("divCharacters").innerHTML = xhr.responseText;
    }
  }
  </script>
 </HEAD>

 <BODY>
  Our first simple AJAX example
  <br><br>
  Make a selection here:
  <br>
  <select onChange="updateCharacters();" id="selShow">
  <option value=""></option>
  <option value="b5">Babylon 5</option>
  </select>
  <br><br>
  In response, a list of characters will appear here:
  <br>
  <div id="divCharacters">
  <select></select>
  <div>
 </BODY>
</HTML>


这是跨域访问XMLHttp的问题吗,应该怎么解决?

------解决方案--------------------
LZ是怎么测试的,通过http服务器,还是直接打开html文件
ajax要通过http服务器才可以使用
  相关解决方案