admin管理员组文章数量:1410724
Right now, I am using curl in PHP to get the HTML source code of some remote web page.
Is there any way I can get the same HTML source code of some cross-domain web page in JavaScript? Any tutorials?
Right now, I am using curl in PHP to get the HTML source code of some remote web page.
Is there any way I can get the same HTML source code of some cross-domain web page in JavaScript? Any tutorials?
Share Improve this question edited Apr 11, 2011 at 1:08 Michael Petrotta 61k27 gold badges152 silver badges181 bronze badges asked Apr 11, 2011 at 1:05 Vinay JeurkarVinay Jeurkar 3,1429 gold badges40 silver badges57 bronze badges 2- Not without the cooperation of the target site. – Reid Commented Apr 11, 2011 at 1:06
- 1 Or without the use of some intermediary that can proxy the target site. – jdigital Commented Apr 11, 2011 at 1:52
3 Answers
Reset to default 2I think you need to know about JSONP to access cross-domain web pages in js
https://stackoverflow./questions/tagged/jsonp?sort=votes
Q. How is this any different than issuing an AJAX "GET http://otherdomain./page.html" call?
A. The same-origin policy checks the HTTP response headers for AJAX requests to remote domains, and if they don't contain a suitable Access-Control-Allow-Origin
header, the request fails.
So, there are two ways to make this work:
If you control the other domain, you can include the following header in the HTTP response:
Access-Control-Allow-Origin: *
(details at MDC)If you don't, you're stuck implementing a server-side proxy (for example, this simple PHP proxy).
In any case, once you implement one of the two options above, you're left with a simple AJAX call:
$.ajax({ url: "http://mydomain./path/to/proxy.php?url="+ encodeURI("http://otherdomain./page.html"), dataType: "text", success: function(result) { $("#result").text(result); } });
This solution I just found might be of use like the other workarounds...
http://www.ajax-cross-domain./
本文标签: Get HTML of another page in another domain using JavaScriptStack Overflow
版权声明:本文标题:Get HTML of another page in another domain using JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744816573a2626732.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论