0

I need to fetch source code from a given url. I've tried using :

var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://google.com', false);
xhr.send(null);
var data=xhr.responseText;

But this does not work for me. So, I tried using ajax given in one of the SO link but it didn't work for me :

$.get( "http://google.com", function( data ) {
        $( ".result" ).html( data );
        alert( "Load was performed." );
    }); 

Can someone please guide me what I am doing wrong here ? Thanks in advance.

Ishan
  • 996
  • 3
  • 13
  • 34
  • did you try using `https` over `http`? – Ritik Saxena Jan 25 '17 at 06:06
  • 1
    If you paste the error into google I bet you will get some useful information on why it isn't working. You're running into the [Same-origin Policy](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy) –  Jan 25 '17 at 06:06
  • It will not work, because you are hitting cross domain and cross domain is restricted – muneebShabbir Jan 25 '17 at 06:07
  • @TinyGiant I've searched for it already and I've tried using CORS extension on google chrome and `xhr.withCredentials = true;` also. – Ishan Jan 25 '17 at 07:09
  • @RitikSaxena yes I've tried it – Ishan Jan 25 '17 at 07:11
  • @muneebShabbir but it should work with CORS extension toggled off, right? – Ishan Jan 25 '17 at 07:13
  • @Ishan But using extension is not a solution. Then your application will work only on chrome with specific extension enable, which is not good – muneebShabbir Jan 25 '17 at 07:21
  • @muneebShabbir yes, you are right. Is there any way to get the source of a url ? I need to send this source to some other link. They are separate applications running on localhost but with different ports. – Ishan Jan 25 '17 at 07:27
  • Set the `Access-Control-Allow-Origin` header on the resource you're trying to request. –  Jan 25 '17 at 07:58

0 Answers0