Javascript or JQuery get html content from externa

2019-01-29 07:48发布

问题:

Possible Duplicate:
Ways to circumvent the same-origin policy

Is there anyway to get content from external site for example i want to get content from url http://www.readwriteweb.com/ in client site by using Javascript or JQuery

回答1:

Here are two options:

  1. Use an iframe to embed a web page of the external site in your page. You can only embed an entire page, including all its advertising and navigation sites, but on some external sites you can find versions of the page intended for mobile or for printing that might look reasonable when embedded.
  2. Use a public CORS proxy. The only one I have found is http://www.corsproxy.com/ . Personally I am suspicious of that site as there is no TOC, privacy policy, or even any indication of who runs the site. However if yours is some small project, where you don't care about privacy, security, reliability, or scaling you might try it. Here is an example of usage adapted from the site:

    $.get( 'http://www.corsproxy.com/en.wikipedia.org/wiki/Http', function(response) { document.body.innerHTML = response; });



回答2:

Unless the site is set up to allow cross-origin requests, you can't access the DOM of another site for security reasons.

This is known as the Same origin policy. There are a number of ways around it if you control the server.

The other option is to pull the data yourself server-side and deliver this to the client from your domain.



回答3:

If your external site is not in the same domain as your site then No. I would suggest you to use JSONP objects to send/receive between sites in different domain.