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

问题描述

我怎样才能使用jquery获得外部URL的HTML?

简短的回答是你不能,因为AJAX请求通过限于相同(子)域和端口。



相同的限制适用于 iframe 元素:您不能创建 iframe

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



显然,使用此解决方案时,可以对URL,图像,样式表等进行相对引用(例如 ../ images / image.gif )将不再起作用,因为它们不在页面上下文中。无论如何,这在你的情况是一个问题是不可能的。一种解决方法可能是使用。

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

解决方案

The short answer is you can't, because AJAX requests are limited to the same (sub)domain and port by the 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.

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.

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中获取外部网址的html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 20:52