|
-
July 3rd, 2007, 07:48 AM
#2
Re: to access a text file on HTTP server from browser using a script
AJAX is not a language, it is merely a manner in passing information between the server and the client using JavaScript and XML. This being said, you can't code in AJAX to do this. You need a server-side language (PHP, ASP.NET, JSP, etc.) to read the textfile and then output the data. JavaScript will then receive this text as the response text. Here is an example with PHP as the server-side language.
whatever.html
Code:
<html>
<body>
<script type="text/javascript">
var AJAX = {
initialize: function(){
var xmlHTTP;
try{xmlHTTP = new XMLHttpRequest();}
catch(e){
try{xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");}
catch(e){
try{xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");}
catch(e){
alert("Your browser does not support AJAX!");
return false;
}
}
}
return xmlHTTP;
},
get: function(url, func){
var obj = this.initialize();
obj.open('GET', url, true);
obj.send(null);
obj.onreadystatechange = function(){
if(obj.readyState==4){
func(obj.responseText);
}
}
}
}
function parseFile(retText){
document.getElementById('theDiv').innerHTML = retText;
}
</script>
<input type="button" value="Get File" onclick="AJAX.get('readfile.php', parseFile)">
<div id="theDiv"></div>
</body>
</html>
readfile.php
PHP Code:
<?php
$file = 'filename.txt';
$content = @file_get_contents($file);
if($content){
echo $content;
}
?>
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|