What am I missing in the XMLHttpRequest?

Posted by user297979 on Stack Overflow See other posts from Stack Overflow or by user297979
Published on 2010-03-20T12:04:13Z Indexed on 2010/03/20 12:11 UTC
Read the original article Hit count: 361

Filed under:
|

Hi guys, I'm completely new to the javascript and ajax world but trying to learn.

Right now I'm testing the XMLHttpRequest and I can't make work even the simplest example. This is the code I'm trying to run

    <script type="text/javascript">
        function test() {
            xhr = new XMLHttpRequest();

            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200){
                    var container = document.getElementById('line');
                    container.innerHTML = xhr.responseText;
                } else {
                    alert(xhr.status);
                }
            }  

            xhr.open('GET', 'http://www.google.com', true);                  
            xhr.send(null); 
        }
    </script>

And I always get the alert with the status 0. I've read tons of webs about this and I don't know what am I missing. I will appreciate any help, thanks!

© Stack Overflow or respective owner

Related posts about xmlhttprequest

Related posts about JavaScript