在 jquery 中获取外部 url 的 html [英] get html of external url in jquery

查看:25
本文介绍了在 jquery 中获取外部 url 的 html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 jquery 获取外部 URL 的 HTML?

How can i get external URL's HTML using jquery?

推荐答案

简短的回答是你不能,因为 AJAX 请求被 同源政策.

The short answer is you can't, because AJAX requests are limited to the same (sub)domain and port by the Same Origin Policy.

同样的限制适用于 iframe 元素:您不能创建一个指向外部页面的 iframe,并从那里获取其 HTML.

The same restrictions apply to iframe elements: You can't create an iframe pointing to the external page, and grab its HTML from there.

通常的方法是使用作为代理的服务器端脚本(例如用 PHP 编写):它获取外部站点的内容并将其提供给 JavaScript.它将必须在与页面相同的域上运行.

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.

显然,使用此解决方案,对 URL、图像、样式表等(例如 ../images/image.gif)的相对引用将不再有效,因为它们在您的页面上与上下文无关.这是否是您的情况的问题是无法确定的.一种解决方法可能是使用 标签.

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.

这篇关于在 jquery 中获取外部 url 的 html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆