Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

get html of external url in jquery

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...