在 jquery 中获取外部 url 的 html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3837717/
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
get html of external url in jquery
提问by hotcoder
How can i get external URL's HTML using jquery?
如何使用 jquery 获取外部 URL 的 HTML?
回答by Pekka
The short answer is you can't, because AJAX requests are limited to the same (sub)domain and port by the Same Origin Policy.
简短的回答是你不能,因为 AJAX 请求被Same Origin Policy限制在同一个(子)域和端口。
The same restrictions apply to iframe
elements: You can't create an iframe
pointing to the external page, and grab its HTML from there.
相同的限制适用于iframe
元素:您不能创建iframe
指向外部页面的指向,并从那里获取其 HTML。
The usual way is to use a server-side script (written e.g. in PHP) that serves as a proxy: It fetches the external site's content and serves it back to JavaScript. It will have to run on the same domain as the page.
通常的方法是使用作为代理的服务器端脚本(例如用 PHP 编写):它获取外部站点的内容并将其返回给 JavaScript。它将必须在与页面相同的域上运行。
Obviously, using this solution, relative references to URLs, images, stylesheets and such (e.g. ../images/image.gif
) will no longer work, as they areout of context on your page. Whether that is a problem in your situation is impossible to tell. One workaround for that may be using a <base>
tag.
显然,使用此解决方案,对 URL、图像、样式表等(例如../images/image.gif
)的相对引用将不再有效,因为它们在您的页面上与上下文无关。在你的情况下这是否是一个问题是不可能的。一种解决方法可能是使用<base>
标签。
回答by Stefanvds
You need jquery $.get
你需要 jquery $.get
http://api.jquery.com/jQuery.get/
http://api.jquery.com/jQuery.get/
Example: Alert out the results from requesting test.cgi with an additional payload of data (HTML or XML, depending on what was returned).
示例:通过额外的数据负载(HTML 或 XML,取决于返回的内容)来提醒请求 test.cgi 的结果。
$.get("test.cgi", { name: "John", time: "2pm" },
function(data){
alert("Data Loaded: " + data);
});
Edit: this only works if your page is on the same domain.
编辑:这仅在您的页面位于同一域中时才有效。
回答by Reigel
回答by Steve Claridge
You could use $.ajax or $.get to make a call to a URL on your own domain and then use whatever server-side language you are using to retrieve the HTML and then return it.
您可以使用 $.ajax 或 $.get 调用您自己域中的 URL,然后使用您使用的任何服务器端语言来检索 HTML,然后返回它。
It's two HTTP requests instead of one but it'll get around your problem.
这是两个 HTTP 请求而不是一个,但它会解决您的问题。
You could also cache the external sites HTML in your backend code so that a request from the Javascript does not always result in two HTTP requests - of course, all depends on how often the HTML you want to grab will change.
您还可以在后端代码中缓存外部站点 HTML,以便来自 Javascript 的请求不会总是导致两个 HTTP 请求 - 当然,这一切都取决于您想要获取的 HTML 更改的频率。
A slight twist on the above would be to have a background task running on your server that retrieves the external HTMl every X seconds and saves it locally. Requests to your domain from your JS just pick up the latest copy from your server. That means your JS request isn't slowed down by waiting for another external HTTP request.
上面的一个小改动是在您的服务器上运行一个后台任务,它每 X 秒检索一次外部 HTMl 并将其保存在本地。从您的 JS 向您的域发出的请求只需从您的服务器中获取最新副本。这意味着您的 JS 请求不会因等待另一个外部 HTTP 请求而减慢。
回答by Hannes
All common Browsers do not allows Javasript Calls to access any Pages with another (sub)Domain because of the Same Origin Policy. The only way to work around that is to set up some kind of "proxy" on your own server (for example an php Script) that runs under the same Domain, gets the Information you want from a third source and prints them out.
由于同源策略,所有常见的浏览器都不允许 Javasript 调用访问具有另一个(子)域的任何页面。解决这个问题的唯一方法是在您自己的服务器上设置某种“代理”(例如一个 php 脚本),它在同一域下运行,从第三个来源获取您想要的信息并将它们打印出来。
回答by Zuhaib
You could add a PHP or any other server side language to your site that could act as a proxy for your script to fetch the page html.
您可以将 PHP 或任何其他服务器端语言添加到您的站点,这些语言可以作为您的脚本的代理来获取页面 html。
You could then call your server side proxy with the url using Ajax which would return you the HTMl of that page.
然后,您可以使用 Ajax 使用 URL 调用服务器端代理,这将返回该页面的 HTMl。