2010-06-21 60 views
0

我有以下代码用于插入另一页进入的div把另一个页面放在div标签中?

的XHTML 1.0严格标准模板

</style> 
<script type="text/javascript"> 
function ajaxFunction(id, url){ 
    var xmlHttp; 
    try {// Firefox, Opera 8.0+, Safari 
     xmlHttp = new XMLHttpRequest();  
    } catch (e) {// Internet Explorer 
     try { 
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
      try { 
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
      } catch (e) { 
       alert("Your browser does not support AJAX!"); 
       return false; 
      } 
     } 
    } 

    xmlHttp.onreadystatechange = function(){ 
     if (xmlHttp.readyState == 4) { 
      //Get the response from the server and extract the section that comes in the body section of the second html page avoid inserting the header part of the second page in your first page's element 
      var respText = xmlHttp.responseText.split('<body>'); 
      elem.innerHTML = respText[0].split('</body>')[0]; 
     } 
    } 

    var elem = document.getElementById(id); 
    if (!elem) { 
     alert('The element with the passed ID doesn\'t exists in your page'); 
     return; 
    } 

    xmlHttp.open("GET", url, true); 
    xmlHttp.send(null); 
}  

HTTP://www.boxyourtvtrial .com /');“/>

但它不能正常工作 我也把谷歌的地址,但仍然它不工作 什么是错的代码....

+0

什么不起作用?你有错误吗?它看起来很奇怪吗? – 2010-06-21 12:31:44

+0

它不会加载指定页面 – rajesh 2010-06-21 12:40:01

回答

1

XMLHTTPRequest不允许做出任何请求域比另一个HTML页面来自于。

Here's some info解决此限制。

+0

那么使用什么来代替 或需要任何更正 – rajesh 2010-06-21 12:38:20

+0

有[多个资源](http://www.google.com/search?q=xmlhttprequest+cross-domain)关于网络上可用的跨域AJAX。 – josh3736 2010-06-21 12:55:58

0

如果要嵌入其他页面,可以使用iframe。但是,当页面来自另一个域时,您将无法从主页面访问iframe的内容。

如果您确实需要嵌入另一个页面,请在您的服务器上加载页面(使用您的语言的HTTP客户端框架),然后将其作为AJAX请求的结果发送给客户端。

请注意,这通常被认为是滥用或没有网站所有者的许可(版权,带宽等)彻底非法。