如何使用 .load() 从另一个域获取内容?
使用 .load() (或任何 jQuery ajax 函数)从我的域中的任何位置请求数据都可以正常工作。
但尝试访问不同域中的 URL 不起作用。你怎么做?另一个域也恰好是我的。
我读到了一个技巧,你可以使用 PHP 做一个获取内容的代理,然后你在服务器上的那个 php 位置上使用 jQuery 的 ajax 函数,但这仍然在你自己的服务器上使用 jQuery ajax,所以这不算数。
有没有好的插件?
编辑:我发现了一个非常好的 jQuery 插件,它允许您使用任何 jQuery 函数从其他页面请求内容,就像在您自己的域中请求普通 ajax 请求一样。
帖子:http://james.padolsey.com/javascript/cross -domain-requests-with-jquery/
插件:https://github.com/jamespadolsey/jQuery-Plugins/tree/master/cross-domain-ajax/
Requesting data from any location on my domain with .load() (or any jQuery ajax functions) works just fine.
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.
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.
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.
The post: http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/
The plugin: https://github.com/jamespadolsey/jQuery-Plugins/tree/master/cross-domain-ajax/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是因为跨域策略,这意味着使用客户端脚本(又名 javascript...)您无法从另一个域请求数据。幸运的是,大多数服务器端脚本中不存在这种限制。
所以...
Javascript:
PHP in "google-html.php":
可以工作。
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.
So...
Javascript:
PHP in "google-html.php":
would work.
就您的浏览器而言,不同的域=不同的服务器。要么使用 JSONP 执行请求,要么使用 PHP 进行代理。您可以使用 jQuery.ajax() 来执行跨域 JSONP 请求。
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.一种非常简单的解决方法是使用雅虎的 YQL 服务,该服务可以从任何外部站点检索内容。
我已经按照这个仅使用 JavaScript 和 YQL 的示例在几个网站上成功完成了此操作。
http://icant.co.uk/articles/crossdomain -ajax-with-jquery/using-yql.html
此示例是博客文章的一部分,该文章还概述了一些其他解决方案。
http://www.wait-till-i.com/2010/01/10/loading-external-content-with-ajax-using-jquery-and-yql/
One really easy workaround is to use Yahoo's YQL service, which can retrieve content from any external site.
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
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/
我知道另一种有效的解决方案。
它不需要您更改 JQuery。它确实要求您可以在您的域中建立一个 ASP 页面。我自己也用过这个方法。
1)创建一个 proxy.asp 页面,如本页 http://www.itbsllc.com /zip/proxyscripts.html
2) 然后你可以执行 JQuery 加载函数并为其提供 proxy.asp?url=.......
该链接上有一个示例,说明如何准确格式化它。
无论如何,您将外部页面 URL 和所需的 mime 类型作为获取变量提供给本地 proxy.asp 页面。我使用的两种 mime 类型是 text/html 和 image/jpg。
请注意,如果您的目标页面包含带有相对源链接的图像,则这些图像可能无法加载。
我希望这有帮助。
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.
1) Create a proxy.asp page like the one on this page 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.
Note, if your target page has images with relative source links those probably won't load.
I hope this helps.