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

4
  • 1
    can you try with var url = "http://localhost/clock.php"; first? Commented Apr 16, 2014 at 4:54
  • why is the document on your desktop and not in the htdocs folder? 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" Commented Apr 16, 2014 at 6:00
  • @user2684308 php.net/manual/en/features.commandline.webserver.php Commented Apr 16, 2014 at 7:05

1 Answer 1

6

You 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:// ).

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

1 Comment

yes, adding the proper protocol would help and did help. I prefixed the path with the HTTP:// and it worked. Thank you all for your help.

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.