0

I am trying to get myself started with Ajax so I was just trying to load some content from a file on click of a button but the content of other file is not loaded

<!DOCTYPE html>
<html>
    <head>
        <script>
            function loadData()
            {
                var xmlhttp;
                if (window.XMLHttpRequest)
                {
                    xmlhttp=new XMLHttpRequest();
                }
                else
                {
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange=function()
                {
                    if (xmlhttp.readyState==4 && xmlhttp.status==200)
                    {                                    
                        document.getElementById("output").innerHTML=xmlhttp.responseText;
                    }
                }
                xmlhttp.open("GET","a1.html",true);
                xmlhttp.send();
            }
        </script>
        </head>
        <body>    
            <h2>AJAX</h2>
            <button type="button" onclick="loadData()">Request data</button>
            <div id="output"></div>    
        </body>
</html>

a1.html file is present under same folder where this html file is.

3
  • Are you sure the path is correct, check again.. Commented Sep 8, 2015 at 11:20
  • please check that "a1.html" file will be in the same folder where the above file is located. Commented Sep 8, 2015 at 11:32
  • Your code is working fine for me. Commented Sep 8, 2015 at 11:32

2 Answers 2

2

I got it , I must request the page from a server. not just localhost

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

Comments

0

Hmmm. everything looks good with this code. Maybe you should check if the file exists. You can also use local files with AJAX and not only urls with a server address.

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.