I am trying to test a simple Ajax script using Javascript and I'm not receiving a response. The PHP script is placed in the htdocs
folder of the local server and the HTML page that includes the Ajax calls is placed on my desktop. A readyState
of 4 is received from the server but the status returned is 0, not 200.
The error generated on Firefox state:
NS_ERROR_DOM_BAD_URI: Access to restricted URI denied.
Here is the section of code responsible for calling the Ajax object:
var myRequest = getXMLHttpRequest();
function callAjax() {
var url = "localhost/clock.php";
var myRandom = parseInt(Math.random()*99999999);
myRequest. open("GET", url + "?rand=" + myRandom, true);
myRequest.onreadystatechange = responseAjax;
myRequest.send(null);
}
function responseAjax() {
if(myRequest.readyState == 4) {
if(myRequest.status == 200) {
var now = new Date();
var localTime = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
var serverTime = myRequest.responseText;
document.getElementById("clock").innerHTML = "Server: " + serverTime + "<br />Local: " + localTime;
} else {
alert("An error has occurred: " + myRequest.statusText);
}
}
}
Can anyone offer insight to my problem?
OSX 10.8.5
MAMP version 2.1.3
I am trying to test a simple Ajax script using Javascript and I'm not receiving a response. The PHP script is placed in the htdocs
folder of the local server and the HTML page that includes the Ajax calls is placed on my desktop. A readyState
of 4 is received from the server but the status returned is 0, not 200.
The error generated on Firefox state:
NS_ERROR_DOM_BAD_URI: Access to restricted URI denied.
Here is the section of code responsible for calling the Ajax object:
var myRequest = getXMLHttpRequest();
function callAjax() {
var url = "localhost/clock.php";
var myRandom = parseInt(Math.random()*99999999);
myRequest. open("GET", url + "?rand=" + myRandom, true);
myRequest.onreadystatechange = responseAjax;
myRequest.send(null);
}
function responseAjax() {
if(myRequest.readyState == 4) {
if(myRequest.status == 200) {
var now = new Date();
var localTime = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
var serverTime = myRequest.responseText;
document.getElementById("clock").innerHTML = "Server: " + serverTime + "<br />Local: " + localTime;
} else {
alert("An error has occurred: " + myRequest.statusText);
}
}
}
Can anyone offer insight to my problem?
OSX 10.8.5
MAMP version 2.1.3
-
1
can you try with
var url = "http://localhost/clock.php";
first? – Scuzzy Commented Apr 16, 2014 at 4:54 - why is the document on your desktop and not in the htdocs folder? – serakfalcon Commented Apr 16, 2014 at 4:57
- You should create site folder in htdocs folder and copy there clock.php then try with this var url="localhost/site_folder/clock.php" – shashank Commented Apr 16, 2014 at 6:00
- @user2684308 php/manual/en/features.mandline.webserver.php – guest271314 Commented Apr 16, 2014 at 7:05
1 Answer
Reset to default 6You say your file is on the desktop. You cannot open a local file ( file:// ) and do ajax. You need to access the file through your Apache server ( http:// ).