1

嗨,实际上我正在尝试获取提供的任何页面的内容...如果页面在同一服务器中,如果我提供另一个服务器名称它不起作用,我可以满足。我知道我们可以在 jquery 中轻松实现,但我仅在js中需要它...在我使用的代码下方...

<script>
 function loadXMLDoc()
 {
  var xmlhttp;
 if (window.XMLHttpRequest)
     {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
     }
   else
    {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
    xmlhttp.onreadystatechange=function()
     {alert(xmlhttp.status);
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
    //alert("SADF");
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
          }
     }
  var url=document.getElementById("url").value;

  xmlhttp.open("GET",url,true);
    xmlhttp.send();
    }
     </script>

<div id="myDiv"></div>
<input type="text" name="url" id="url"/>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

提前致谢...

4

1 回答 1

-1

您不能从 Javascript 发出跨域请求。有两种解决方案;

  1. 要么为此使用 IFrame
  2. 使用 XDomainRequest 发出请求

有关 XDomainRequest 的更多详细信息,请参阅此链接:

MSDN

于 2013-08-13T06:10:43.517 回答