0

I want to fetch the content of a file in server without extension using AJAX. Simple demonstration of code is as follows:

<html>
<head>
<script>

function read_my_file(){

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","MYFILE",true);// MYFILE HAS NO EXTENSION
xmlhttp.send();

}
</script>

</head>

<body>

<h2>THIS FETCHES DATA FROM SERVER IF WORKS!!!!!!</h2>
<button type="button" onclick="read_my_file()">Request data</button>
<div id="myDiv"></div>

</body>

</html>

As the file MYFILE has no extension, I think it is interpreted as folder name and I get 404 error. How can I fix this?

5
  • 1
    Give this a shot: './MYFILE'. Where is MYFILE located in relation to your page; same directory? Commented May 8, 2014 at 19:02
  • It is not in a same directory. Commented May 8, 2014 at 19:06
  • Well, that'll be why it can't load it; xmlhttp doesn't know where to find it! Where is it located in relation to your page? Commented May 8, 2014 at 19:07
  • No, "MYFILE" has just there for illustration. Actually in real, I used the full path, "/abs/gdf/MYFILE". Commented May 8, 2014 at 19:20
  • And those are the magic words. Basically, you have to run an AJAX call to a file that's accessible to the web browser. Check here: stackoverflow.com/questions/1275262/… Commented May 8, 2014 at 19:22

2 Answers 2

1

If chris's answer does not work you could also try and specify what mime-type your page should have using:

xmlHttp.overrideMimeType("application/xml");

or

xmlHttp.setRequestHeader("Accept", "text/html");

and simply replace "application/xml" or "text/html" with the type you need.

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

2 Comments

Hmmm... try setting the mimetype to application/octet-stream, i think that is suppose to be raw data...
Although it seems as chris has given you the reason why above, instead of using absolute path try using relative. That is you use ../../basefolder/theOtherFolder/yourFile.
0

If you want to load a file directly, it must be located within the web server's document root. That is, if the document root is /var/www/example.com/public, then you can access files like this:

  • /var/www/example.com/public/json/myfile.json
    • xmlhttp.open("GET","json/myfile.json",true);
    • xmlhttp.open("GET","http://www.example.com/json/myfile.json",true);

It can't, however, be accessed if the file is located at /var/files/json/myfile.json, since that lies outside the document root! You'd have to use a server-side language (such as PHP) to read the file.

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.