Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to send HTTP request like a simple request for "http://google.com/" and then print the HTTP response headers on screen. How can it be done? I want the base code so that I can use it to send more complex GET and POST requests.

<html>
<body>

<script type="text/javascript">
function sendgetreq()
{
    var req = new XMLHttpRequest();
    req.open('GET', "https://www.google.com/search?q=asd", true);
    req.send(null);
    var headers = req.getAllResponseHeaders().toLowerCase();
    //document.write("Headers are:"+headers);
    alert(headers);
}
</script>
<INPUT TYPE=BUTTON OnClick="sendgetreq();" VALUE="Send Request">

</body>
</html>

This shows me an empty popup box.

share|improve this question
    
    
w3schools.com/dom/… –  Shashank Sep 24 '13 at 4:47
    
Thanks @Ajeet for the quick reply. I had tried both but they didn't work for me. Check out the code I have posted now. –  Sonia Sharma Sep 24 '13 at 5:02
    
The link i posted will get you headers for your own domain only as cross domain restrictions will apply for external domains. –  Ajeet Sinha Sep 24 '13 at 5:13
    
@Ajeet: But I want to send a GET request to google.com and fetch the response header. –  Sonia Sharma Sep 24 '13 at 5:16

1 Answer 1

You can't use XMLHttpRequest cross domain unless requested server explicitly allow it and google.com doesn't explicitly allow it. However you may disable the web security feature of your browser, and then your same code will start working.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.