2012-02-10 112 views
4

在下面的代碼jQuery的負載不工作。我在這裏錯過了什麼?jQuery的負載不工作

<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"> </script> 
</head> 
<body style="font-size:62.5%;"> 

<div id="dialog" title="Dialog Title">I'm in a dialog</div> 
<script> 
$(document).ready(function() { 
     $("#dialog").load('http://www.google.com/intl/en/about/index.html #maia-main'); 
}); 
</script> 
</body> 
</html> 
+2

無法執行出於安全原因,跨站點AJAX請求。你必須爲此使用iframe。 – 2012-02-10 22:56:29

回答

5

您正在請求位於不同域的頁面,以便應用跨域策略。您只能訪問數據跨域如果遠程服務器允許它(且僅限使用JSONP我相信,任何人都請糾正我,如果我錯了,在這個)。如果你想抓住谷歌的頁面的源代碼,您將需要一個服務器端腳本來完成它作爲您的jQuery代理:

$(function() { 

    //notice the client-side code (JS) is requesting a page on the save domain 
    $("#dialog").load('my-script.php #maia-main'); 
}); 

而且在my-script.php你會抓住你想要的遠程頁面:

<?php 

//PHP, like all server-side languages has no cross-domain-policy 
echo file_get_contents('http://www.google.com/intl/en/about/index.html'); 
?> 

文檔爲file_get_contents()http://www.php.net/file_get_contents

2

嘗試將文件移到服務器,jQuery的AJAX不是本地總是工作。

0

腳本執行

當使用URL不具有後綴選擇表達主叫​​,內容被傳遞到.html()被移除腳本之前。這會在腳本塊被丟棄之前執行。如果​​調用與附加到URL選擇表達,然而,腳本剝離出來之前DOM被更新,並因此不會執行。的兩種情況下的一個例子在下面可以看到:

在此,加載到#a作爲文檔將成功執行的一部分的任何JavaScript。

$('#a').load('article.html'); 

然而,在以下的情況下,在文檔中的腳本塊被加載到#b被剝離出來,不執行:

$('#b').load('article.html #target'); 

來源:jQuery.com

0

要加載外部鏈路in「.load()」

創建頁面說external.php

在external.php把下面的代碼:

<?php 
$url = 'http://ur/url/here'; 
echo $var = get_file_contents($url); ?> 

現在加載這個頁面中的jQueryñ它會加載外部鏈接

$( '格')負載( 'external.php')。