-2

I am trying to a simple HTML page working the text file is in the same directory but it does not want to work (ie unresponsive button). I did look at numerous examples but none of them shed light to the issue. The text file is in the same directory, I have tried absolute and relative paths. Is it possible that due to my orignation and request location being the same AJAX would shut down.

    <!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
// code for IE7+, Firefox, Chrome, Opera, Safari
var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","\test.txt",true);
xmlhttp.send();
document.getElementById("myDiv").innerHTML=xmlhttp.responseText
}
</script>
</head>
<body>

<div id="myDiv"><h2>By the power of AJAX!!!!!!!</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

UPDATE corrected error on the cut and paste. By not working I mean the button does nothing. I have looked at other examples and none of them seem to work. When I try and run from my drive but work from the webpage. I do not have a server but I am only using text so I should not need anything beyond this.

TheCodeNovice
  • 750
  • 14
  • 35

2 Answers2

1

The content of the file must be loaded using a HTTP call as the xmlhttp.send() method makes a call to the server. Check that the xmlhttp.open("GET","\test.txt",true); method signature expect the second parameter as an URL to the server and the first argument specify the HTTP method (GET/POST)
What is the location of the text file from this page you are running, your code will try to load it from the same dir.

user2720864
  • 8,015
  • 5
  • 48
  • 60
  • That was an error due to the cut and paste and trying to get the code to show right in the post. It is not the problem added back in. The text file is in the same folder as the HTML page I have tried different ways of referencing the test. Full absolute path, relative path and just the file name. None of them work. – TheCodeNovice Sep 10 '13 at 18:29
  • `xmlhttp.open` method expect the 2nd argument as an URL. How are u running the page ? If u r running it from a local server keep the .txt file in the same dir. Otherwise you must use an `URL` NOT a Path to access the test.txt file – user2720864 Sep 10 '13 at 18:35
  • the files are on my hard drive and I am accessing the webpage by simply typing the file location in the address bar (d:\blah\blah\testpage.html). I assume the request would have to just be the file name. – TheCodeNovice Sep 10 '13 at 19:04
  • When you say xmlhttp.send it means you are sending a request to server.So the file test.txt must be called using a URL if you are running the .html file from disk. In other word it must make a HTTP call to get the content and that's the reason why you need to specify the HTTP method (GET/POST) also. Check [this](http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp) – user2720864 Sep 10 '13 at 19:59
  • I got the html from that site. I literally cut and pasted from that site and it will not work properly. I appreciate the help. I know it seems like a simple task but I cannot get it to work. – TheCodeNovice Sep 10 '13 at 20:17
  • The only difference is they are running the script from within a server and you are running it from your local drive. check the accepted answer from this http://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin – user2720864 Sep 10 '13 at 20:27
0

I had the same problem, I read everywhere and finally i got a solution. The only difference is that i'm using a local server. The solution is put all the dir after the dir that the server is running till the file name. So, the server run in the "localhost/xampp/htdocs/project". This is my code.

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()
  {  alert(xmlhttp.status+" status"+"\n"+xmlhttp.readyState+" ready");
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {   alert(xmlhttp.responseText);
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }

xmlhttp.open("GET","views/seila.txt",true);
xmlhttp.send();
}