javascript 如何使用 .load() 从另一个域获取内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5320511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you get content from another domain with .load()?
提问by trusktr
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/
回答by mattsven
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.
这是因为跨域策略,这意味着使用客户端脚本(又名 javascript...)您不能从另一个域请求数据。幸运的是,这个限制在大多数服务器端脚本中不存在。
So...
所以...
Javascript:
Javascript:
$("#google-html").load("google-html.php");
PHP in "google-html.php":
“google-html.php”中的PHP:
echo file_get_contents("http://www.google.com/");
would work.
会工作。
回答by Andrew Moore
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 请求。
回答by Ted Avery
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/
回答by user1991791
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 类型作为获取变量提供给您的本地 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.
请注意,如果您的目标页面具有带有相关源链接的图像,则这些图像可能无法加载。我希望这有帮助。