How to get the content of a remote page with JavaScript?

丶灬走出姿态 提交于 2019-11-28 12:25:16

Same domain policy is going to get you.

1) Proxy through your server. browser->your server->their server->your server->browser.

2) Use flash or silverlight. The 3rd party has to give you access. The bridge between javascript and flash isn't great for large amounts of data and there are bugs. Silverlight isn't ubiquitous like flash...

3) use a tag. This really isn't safe... Only works if 3rd party content is valid javascript.

Whats about load an PHP Script via AJAX which does file_get_contents() ? This should work for different domain. If i understand correct.

Writing a server-side script that will retrieve the page's content for you is the way to go. You can use the XMLHttpRequest object to make an AJAX call to that script, which will just put through all html (?) for you.

Still, I advise against it. I don't know exactly how much you trust the other site, but the same origin policy exists for a reason. What is it exactly you are trying to do? Usually, there is a workaround.

I dont think you can do this according to the constraints of same origin policy. Two communicate between two domains using Iframes also we can use JS code but both domains need to have communicating code in them. The Child frame can contact the grandparent frame (window) but not here.

Since you are referring to some other url all togeather.

The only way is to do it using your server side code to access the content on the other domain.

Just use PHP:

<?php
$url = "http://www.domaintoretrieve.com";

ob_start();
include_once( $url );

$html = ob_get_contents();
ob_end_clean();

?>

$html contains the entire page to manipulate as needed.

The XMLHTTPRequest object is common to most modern browsers and is what powers AJAX web applications.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!