views:

38

answers:

6

Hi,

I want to extract the source code of a webpage which is hosted by other website, but the problem is that O get an empty response, I tried to pull the source of multiple websites but the problem is from my code:

$(document).ready(function(){
    $.get('http://www.xxxx.com', function(xdata) {
        alert("content: "+xdata);
    });
});

Is there any mistake?

Note: when I try to get the source of a local page, it works, but I don't know why it doesn't for an external one

Thanks

A: 

you cant use AJAX across domains

Pharabus
Is there something else similar in javascript
David
Bauer's answer of using a cross domain proxy is the only way
Pharabus
+1  A: 

This isn't allowed, according to the Same Origin Policy.

The only way to approach this is to use some server-side pull of the data, which you would then process using your AJAX requests, this is known as a Cross-Domain Proxy.

Bauer
A: 

You cannot use contain from an other domain because of the same origin policy

please look into JsonP.

RageZ
A: 

Access-Control-Allow-Origin: * header need to set on external site to do cross domain access.

S.Mark
A: 

Because of the SOP (same origin policy), You can't use URL's from other domains. Try accessing a page from local server and don't use http.

Teja Kantamneni
A: 

If you're not interested in building your own proxy, there's a very easy-to-use public proxy (hosted on AppEngine) for this, with a JavaScript library. CurlJS: http://curljs.azoffdesign.com/

Your example could be done like this (after including the library):

curl("http://www.xxxx.com", function (status, xdata) {
  alert("content:" + xdata);
});

Hope that helps!

bcherry