最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Testing Ajax using localhost - Stack Overflow

programmeradmin1浏览0评论

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

Share Improve this question edited Apr 16, 2014 at 4:56 Mark Bell 29.8k26 gold badges121 silver badges150 bronze badges asked Apr 16, 2014 at 4:50 user2684308user2684308 531 gold badge1 silver badge6 bronze badges 4
  • 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
Add a ment  | 

1 Answer 1

Reset to default 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:// ).

发布评论

评论列表(0)

  1. 暂无评论