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
Add a ment  | 

3 Answers 3

Reset to default 2

I 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