如何使用.load()从其他域获取内容?

时间:2022-06-25 08:18:47

Requesting data from any location on my domain with .load() (or any jQuery ajax functions) works just fine.

使用.load()(或任何jQuery ajax函数)从我的域上的任何位置请求数据都可以正常工作。

Trying to access a URL in a different domain doesn't work though. How do you do it? The other domain also happens to be mine.

尝试访问其他域中的URL不起作用。你怎么做呢?另一个域也恰好是我的。

I read about a trick you can do with PHP and making a proxy that gets the content, then you use jQuery's ajax functions, on that php location on your server, but that's still using jQuery ajax on your own server so that doesn't count.

我读到了你可以用PHP做的一个技巧,并制作一个获取内容的代理,然后你在服务器上的php位置使用jQuery的ajax函数,但是仍然在你自己的服务器上使用jQuery ajax,这样就不算了。

Is there a good plugin?

有一个很好的插件吗?

EDIT: I found a very nice plugin for jQuery that allows you to request content from other pages using any of the jQuery function in just the same way you would a normal ajax request in your own domain.

编辑:我发现了一个非常好的jQuery插件,允许您使用任何jQuery函数从其他页面请求内容,就像您在自己的域中的普通ajax请求一样。

The post: http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/

帖子:http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/

The plugin: https://github.com/jamespadolsey/jQuery-Plugins/tree/master/cross-domain-ajax/

该插件:https://github.com/jamespadolsey/jQuery-Plugins/tree/master/cross-domain-ajax/

4 个解决方案

#1


10  

This is because of the cross-domain policy, which, in sort, means that using a client-side script (a.k.a. javascript...) you cannot request data from another domain. Lucky for us, this restriction does not exist in most server-side scripts.

这是因为跨域策略,在排序中,意味着使用客户端脚本(a.k.a.javascript ...),您无法从另一个域请求数据。幸运的是,大多数服务器端脚本都不存在此限制。

So...

所以...

Javascript:

使用Javascript:

$("#google-html").load("google-html.php");

PHP in "google-html.php":

PHP中的“google-html.php”:

echo file_get_contents("http://www.google.com/");

would work.

会工作。

#2


3  

Different domains = different servers as far as your browser is concerned. Either use JSONP to do the request or use PHP to proxy. You can use jQuery.ajax() to do a cross-domain JSONP request.

就浏览器而言,不同的域=不同的服务器。使用JSONP来执行请求或使用PHP代理。您可以使用jQuery.ajax()来执行跨域JSONP请求。

#3


3  

One really easy workaround is to use Yahoo's YQL service, which can retrieve content from any external site.

一个非常简单的解决方法是使用Yahoo的YQL服务,该服务可以从任何外部站点检索内容。

I've successfully done this on a few sites following this example which uses just JavaScript and YQL. http://icant.co.uk/articles/crossdomain-ajax-with-jquery/using-yql.html

我在这个仅使用JavaScript和YQL的示例的几个站点上成功完成了此操作。 http://icant.co.uk/articles/crossdomain-ajax-with-jquery/using-yql.html

This example is a part of a blog post which outlines a few other solutions as well. http://www.wait-till-i.com/2010/01/10/loading-external-content-with-ajax-using-jquery-and-yql/

此示例是博客文章的一部分,其中还概述了其他一些解决方案。 http://www.wait-till-i.com/2010/01/10/loading-external-content-with-ajax-using-jquery-and-yql/

#4


1  

I know of another solution which works. It does not require that you alter JQuery. It does require that you can stand up an ASP page in your domain. I have used this method myself.

我知道另一个有效的解决方案。它不需要你改变JQuery。它确实要求您可以在您的域中站起来ASP页面。我自己用过这个方法。

1) Create a proxy.asp page like the one on this page http://www.itbsllc.com/zip/proxyscripts.html

1)创建一个proxy.asp页面,如本页http://www.itbsllc.com/zip/proxyscripts.html

2) You can then do a JQuery load function and feed it proxy.asp?url=....... there is an example on that link of how exactly to format it. Anyway, you feed the foreign page URL and your desired mime type as get variables to your local proxy.asp page. The two mime types I have used are text/html and image/jpg.

2)然后你可以做一个JQuery加载函数并将其提供给proxy.asp?url = .......在该链接上有一个如何格式化它的例子。无论如何,您将外部页面URL和所需的mime类型作为get变量提供给本地proxy.asp页面。我使用的两种mime类型是text / html和image / jpg。

Note, if your target page has images with relative source links those probably won't load. I hope this helps.

请注意,如果您的目标页面包含具有相对源链接的图像,则可能无法加载。我希望这有帮助。

#1


10  

This is because of the cross-domain policy, which, in sort, means that using a client-side script (a.k.a. javascript...) you cannot request data from another domain. Lucky for us, this restriction does not exist in most server-side scripts.

这是因为跨域策略,在排序中,意味着使用客户端脚本(a.k.a.javascript ...),您无法从另一个域请求数据。幸运的是,大多数服务器端脚本都不存在此限制。

So...

所以...

Javascript:

使用Javascript:

$("#google-html").load("google-html.php");

PHP in "google-html.php":

PHP中的“google-html.php”:

echo file_get_contents("http://www.google.com/");

would work.

会工作。

#2


3  

Different domains = different servers as far as your browser is concerned. Either use JSONP to do the request or use PHP to proxy. You can use jQuery.ajax() to do a cross-domain JSONP request.

就浏览器而言,不同的域=不同的服务器。使用JSONP来执行请求或使用PHP代理。您可以使用jQuery.ajax()来执行跨域JSONP请求。

#3


3  

One really easy workaround is to use Yahoo's YQL service, which can retrieve content from any external site.

一个非常简单的解决方法是使用Yahoo的YQL服务,该服务可以从任何外部站点检索内容。

I've successfully done this on a few sites following this example which uses just JavaScript and YQL. http://icant.co.uk/articles/crossdomain-ajax-with-jquery/using-yql.html

我在这个仅使用JavaScript和YQL的示例的几个站点上成功完成了此操作。 http://icant.co.uk/articles/crossdomain-ajax-with-jquery/using-yql.html

This example is a part of a blog post which outlines a few other solutions as well. http://www.wait-till-i.com/2010/01/10/loading-external-content-with-ajax-using-jquery-and-yql/

此示例是博客文章的一部分,其中还概述了其他一些解决方案。 http://www.wait-till-i.com/2010/01/10/loading-external-content-with-ajax-using-jquery-and-yql/

#4


1  

I know of another solution which works. It does not require that you alter JQuery. It does require that you can stand up an ASP page in your domain. I have used this method myself.

我知道另一个有效的解决方案。它不需要你改变JQuery。它确实要求您可以在您的域中站起来ASP页面。我自己用过这个方法。

1) Create a proxy.asp page like the one on this page http://www.itbsllc.com/zip/proxyscripts.html

1)创建一个proxy.asp页面,如本页http://www.itbsllc.com/zip/proxyscripts.html

2) You can then do a JQuery load function and feed it proxy.asp?url=....... there is an example on that link of how exactly to format it. Anyway, you feed the foreign page URL and your desired mime type as get variables to your local proxy.asp page. The two mime types I have used are text/html and image/jpg.

2)然后你可以做一个JQuery加载函数并将其提供给proxy.asp?url = .......在该链接上有一个如何格式化它的例子。无论如何,您将外部页面URL和所需的mime类型作为get变量提供给本地proxy.asp页面。我使用的两种mime类型是text / html和image / jpg。

Note, if your target page has images with relative source links those probably won't load. I hope this helps.

请注意,如果您的目标页面包含具有相对源链接的图像,则可能无法加载。我希望这有帮助。