2

Im newbie to AJAX and trying to run this below piece of code but its not working...The Ajax_info.txt file is located in my local drive.

<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","c:/python27/ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
user1050619
  • 19,822
  • 85
  • 237
  • 413
  • You might want to have a look at http://stackoverflow.com/questions/7683596/xmlhttprequest-for-local-files though if you're learning AJAX you'll probably want to use a local web server and load the file from it instead of the file system. – tvanfosson Dec 20 '12 at 03:51
  • If any of the below answers presented a solution to your question, please accept their answer by clicking on the check mark beside their answer. This will help future users searching for an answer to the same question. Thank you. – SnareChops Oct 06 '14 at 02:41

3 Answers3

0

I had the same problems. I removed the if (xmlhttp.readyState==4 && xmlhttp.status==200) and it worked great.

SnareChops
  • 13,175
  • 9
  • 69
  • 91
0

Generally, a browser won't open a local file due to security issues. Depending on the browser, I suppose it might open it if the referencing HTML file is also loaded locally, since technically that would be in the same domain. Generally, you would be using a file on a web server (even if the web server were hosted locally) when using AJAX. In any event, if it does work, you'll receive a status 0, status 200, from a local file. Status 200 is an HTTP protocol status which wouldn't be returned for a local file since it's not being loaded via HTTP.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
0

Please see this code and run you have to give parameter like this

    xmlhttp.open("GET","?c:/python27/ajax_info.txt",true);

and tell me what you want to display in your div

                <!DOCTYPE html>
            <html>
            <head>
            <script>
            function loadXMLDoc()
            {

            var xmlhttp;
            if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
              }
            else
              {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
            xmlhttp.onreadystatechange=function()
              {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {

                var str = xmlhttp.responseText
                alert(str)
                document.getElementById("myDiv").innerHTML="change";
                }
              }
            xmlhttp.open("GET","?c:/python27/ajax_info.txt",true);
            xmlhttp.send(null);
            }
            </script>
            </head>
            <body>


            <button type="button" onclick="loadXMLDoc()">Change Content</button>
            <div id="myDiv" >Let AJAX change this text</div>
            </body>
            </html>
Manish Nagar
  • 1,038
  • 7
  • 12