0

I want to print in console content that are returned in http response. For example when I go to https://google.com and in the tab Network I can see content such as scripts, text, document, png. I want to print names of all .png files. I tried to use something like that:

function hand () {
        console.log(this.getResponseHeader('content-type'));
}
var x = new XMLHttpRequest();
x.open('GET', 'https://google.com', true);
x.onreadystatechange = hand;

x.send(); 

But it doesn't work for me. This action below is assigned for the button on me page.

6
  • Your problem is Same Origin Policy Commented Jul 6, 2015 at 15:31
  • Are you make a ajax call in the same domain or different? Commented Jul 6, 2015 at 15:34
  • @Mike OP specified google.com Commented Jul 6, 2015 at 15:35
  • @Mike It is not ajax call Commented Jul 6, 2015 at 15:40
  • @Mike It was just action on the button. Sorry, I'm new for this. Should I use ajax? Commented Jul 6, 2015 at 15:51

1 Answer 1

1

Cross-origin requests simply are not allowed by default. The remote server may provide permission to your application through CORS or by supporting Ajax alternatives like JSONP.

Edited:

The only (easy) way to get cross-domain data using AJAX is to use a server side language as the proxy as Andy E noted. Here's a small sample how to implement that using jQuery:

The jQuery part:

$.ajax({
    url: 'proxy.php',
    type: 'POST',
    data: {
        address: 'http://www.google.com'
    },
    success: function(response) {
        // response now contains full HTML of google.com
    }
});

Simple as that. Just be aware of what you can or cannot do with the scraped data and be very much aware that such a proxy is a severe security hole. At least make a list of acceptable addresses and don't just blindly accept any passed address. Have a look at a decent proxy script here: PHP Simple Proxy

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.