我正在使用此代码从php文件中获取一些数据。该PHP文件有非常简单的编码,我已检查它是否有效地工作。它看起来像是我的JavaScript代码中没有发送请求的错误。我的AJAX代码下面贴:Ajax请求不会触发。为什么?
var jax=new XMLHttpRequest();
jax.onreadystatechange = function() {
if (jax.readyState == 4 && jax.status == 200)
alert(jax.responseText);
}
jax.open("GET","http://marked.byethost12.com/response.php?req=1&rnd="+Math.random(),true);
jax.send();
自PHP文件中的代码是这样的:
<?php
$request=$_GET["req"];
if($request=="1") //requesting the initiation of protocol
echo 'alert("hello. the protocol has been initiated!")';
else
echo "alert! error in req variable. variable not present or value is not 1";
?>
脚本的地址(包括子域,目录等)是什么?浏览器的开发人员控制台中是否有任何错误? –
我正在使用本地硬盘上的脚本(ajax文件),未上传到任何Web服务器。 Firefox的开发控制台中的错误字符串会这样读取:***跨域请求被阻止:相同的源策略不允许读取http://marked.byethost12.com/response.php?req=1&rnd=0.1276921559338826处的远程资源。 (原因:缺少CORS头'Access-Control-Allow-Origin')*** –
您可能遇到跨域问题。看看这个:http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain – Marks