2010-06-21 62 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請求的結果發送給客戶端。

請注意,這通常被認爲是濫用或沒有網站所有者的許可(版權,帶寬等)徹底非法。