无法使用jQuery.load将外部页面加载到我页面的div中

无法使用jQuery.load将外部页面加载到我页面的div中

问题描述:

我无法将外部html页面加载到页面的div中.

I am not able to load an external html page into a div in my page.

我的Jquery代码是:

My Jquery Code is:

$(document).ready(function(){
     var url = 'http://www.google.com';
     $.get(url, function(response) {
          $('div#external').html(response);
     });
 });

我的HTML页面是

<html><body><div id="external"></div></body></html>

我也尝试使用其他JQuery代码

I also tried using another JQuery code

$(document).ready(function() {
    $('#external').load('http://google.com');
});    

任何人都可以帮助我.

Could anyone please help me.

谢谢 阿马尔(Amal)

Thanks Amal

由于浏览器的限制,大多数Ajax请求都受相同来源策略"的约束.这意味着在大多数情况下,如果不使用Proxy,YQL,JSONP或同等技术来解决此问题,就无法使用jQuery的ajax方法从外部域获取数据.

Due to browser restrictions, most Ajax requests are subject to the "same origin policy". That means that in most cases, you can’t use jQuerys ajax methods to fetch data from external domains without using a Proxy, YQL, JSONP or equivalent technique to get around this.

纯javascript选项是Yahoo的YQL服务.有一个扩展了jQuery.ajax以允许外部域的插件: https://github.com/padolsey/jQuery-Plugins/blob/master/cross-domain-ajax/jquery.xdomainajax.js

A pure javascript option is Yahoo’s YQL service. There is a plugin that extends jQuery.ajax to allow external domains: https://github.com/padolsey/jQuery-Plugins/blob/master/cross-domain-ajax/jquery.xdomainajax.js

使用此插件应允许您在问题中使用ajax示例.

Using this plugin should allow the ajax example in your question.

另一种选择是使用服务器端代理,然后使用ajax请求该页面.如果您的服务器可以运行PHP,请尝试使用"php ajax代理"之类的工具进行搜索,您会得到很多结果.

Another option is to use a server-side proxy and then request that page using ajax. If your server can run PHP, try googling for something like "php ajax proxy" and you’ll get plenty results.