CodeOverload
CodeOverload

Reputation: 48565

Help! Ajax not getting an external page content?

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

Upvotes: 0

Views: 441

Answers (7)

Shanimal
Shanimal

Reputation: 11718

I have a server using a virtual domain and created an apache proxy.

Super fast, works, no quirks.

Copy this, fix paths (mod_proxy, domains, etc...), add to your .conf file, restart server

LoadModule proxy_module /usr/local/zend/apache2/modules/mod_proxy.so
LoadModule proxy_http_module /usr/local/zend/apache2/modules/mod_proxy_http.so
ProxyRequests Off
ProxyPreserveHost On 
ProxyPass /datadomain http://datadomain.com/webservices
ProxyPassReverse /datadomain http://datadomain.com/webservices

Now http://datadomain.com/webservices/data.php = http://yourdomain.com/datadomain/data.php

Enjoy!!!

Upvotes: 0

bcherry
bcherry

Reputation: 7238

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!

Upvotes: 0

Teja Kantamneni
Teja Kantamneni

Reputation: 17492

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.

Upvotes: 0

YOU
YOU

Reputation: 123937

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

Upvotes: 0

RageZ
RageZ

Reputation: 27323

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

please look into JsonP.

Upvotes: 0

gpmcadam
gpmcadam

Reputation: 6580

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.

Upvotes: 1

Pharabus
Pharabus

Reputation: 6062

you cant use AJAX across domains

Upvotes: 0

Related Questions