Jquery load doesn't work

筅森魡賤 提交于 2019-12-01 21:39:00

问题


The jquery load in the below code doesn't work. What I'm missing here?

<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>

回答1:


You are requesting a page that is on a different domain so Cross-Domain-Policies apply. You can only access data cross-domain if the remote-server allows it (and only using JSONP I believe, anyone please correct me if I'm wrong on this). If you want to grab the source of a Google page, you will need to do it with a server-side script as a proxy for your jQuery:

$(function() {

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

And in my-script.php you would grab the remote page you want:

<?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');
?>

Docs for file_get_contents(): http://www.php.net/file_get_contents




回答2:


try to move your script to server, jquery ajax not work always on local.




回答3:


Script Execution

When calling .load() using a URL without a suffixed selector expression, the content is passed to .html() prior to scripts being removed. This executes the script blocks before they are discarded. If .load() is called with a selector expression appended to the URL, however, the scripts are stripped out prior to the DOM being updated, and thus are not executed. An example of both cases can be seen below:

Here, any JavaScript loaded into #a as a part of the document will successfully execute.

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

However, in the following case, script blocks in the document being loaded into #b are stripped out and not executed:

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

Source: jQuery.com




回答4:


To load external link in ".load()"

create page say external.php

In external.php put following code:

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

now load this page in jquery n it will load external link

$('div').load('external.php');



来源:https://stackoverflow.com/questions/9235945/jquery-load-doesnt-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!