I have an ajax setup like this:
var $ = function(id) {return document.getElementById(id);};
var ajax = {
send: function(url, callback, method, data, async) {
var x = new XMLHttpRequest();
x.open(method, url, async);
x.onreadystatechange = function() {
if (x.readyState == 4) {
callback(x.responseText, x.status);
}
};
if (method == "POST") {
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}
x.send(data);
},
query: function(data) {
var query = [];
for (var key in data) {
query.push(encodeURIComponent(key) + "=" + encodeURIComponent(data[key]));
}
return query.join("&");
},
get: function(url, data, callback, async) {
ajax.send(url + "?" + ajax.query(data), callback, "GET", null, async);
},
post: function(url, data, callback, async) {
ajax.send(url, callback, "POST", ajax.query(data), async);
}
}
and this is my function that I call to recieve the contents of a txt file:
function update(text) {
var d = $("display");
ajax.get("chat.txt", {}, function(t) {
d.innerHTML = t;
d.scrollTop = d.scrollHeight;
}, true);
}
but this won't work and always show an old text that was in the txt an hour ago.
So I have found another solution:
function update(text) {
var d = $("display");
ajax.get("display.php", {}, function(t) {
d.innerHTML = t;
d.scrollTop = d.scrollHeight;
}, true);
}
, to call a PHP file which code is:
<?php
echo file_get_contents("chat.txt");
?>
Why can't I just do a call to the txt???
It have worked before in other php projects.
I use WAMP Server 2.
Considering you're using WAMP on localhost, give your /www/ directory full permissions by right clicking on it and clicking on Properties then Security.