Javascript 使用localhost测试Ajax

Javascript 使用localhost测试Ajax,javascript,php,ajax,Javascript,Php,Ajax,我正在尝试使用Javascript测试一个简单的Ajax脚本,但没有收到响应。PHP脚本放在本地服务器的htdocs文件夹中,包含Ajax调用的HTML页面放在我的桌面上。从服务器接收到4的readyState,但返回的状态是0,而不是200 Firefox上生成的错误状态为: NS\u错误\u DOM\u错误\u URI:对受限URI的访问被拒绝。 下面是负责调用Ajax对象的代码部分: var myRequest = getXMLHttpRequest(); function callAj

我正在尝试使用Javascript测试一个简单的Ajax脚本,但没有收到响应。PHP脚本放在本地服务器的
htdocs
文件夹中,包含Ajax调用的HTML页面放在我的桌面上。从服务器接收到4的
readyState
,但返回的状态是0,而不是200

Firefox上生成的错误状态为:

NS\u错误\u DOM\u错误\u URI:对受限URI的访问被拒绝。

下面是负责调用Ajax对象的代码部分:

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);
        }
    }
}
var myRequest=getXMLHttpRequest();
函数callAjax(){
var url=“localhost/clock.php”;
var myRandom=parseInt(Math.random()*9999999);
myRequest.open(“GET”,url+“?rand=“+myRandom,true”);
myRequest.onreadystatechange=responseAjax;
myRequest.send(空);
}
函数responseAjax(){
if(myRequest.readyState==4){
如果(myRequest.status==200){
var now=新日期();
var localTime=now.getHours()+”:“+now.getMinutes()+”:“+now.getSeconds();
var serverTime=myRequest.responseText;
document.getElementById(“时钟”).innerHTML=“服务器:”+serverTime+“
本地:”+localTime; }否则{ 警报(“出现错误:+myRequest.statusText”); } } }
有人能对我的问题提出见解吗

OSX 10.8.5

MAMP版本2.1.3

您说您的文件在桌面上。无法打开本地文件(文件://)并执行ajax操作。您需要通过Apache服务器(http://)访问该文件

你能试试
var url=”吗http://localhost/clock.php";首先?为什么文档在您的桌面上而不在htdocs文件夹中?您应该在htdocs文件夹中创建站点文件夹,并将clock.php复制到那里,然后尝试使用此var url=“”@user2684308是的,添加适当的协议将有帮助,并且确实有帮助。我在路径前面加了HTTP://前缀,它就成功了。谢谢大家的帮助。