request.status=0是为什么?php、浏览器返回都是没有问题

request.status=0,检查了好几遍没发现哪里有问题,代码截图如下
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>Document</title>
</head>
<style>
p {
  color:#ff0000;
}
</style>
<body>
<h1>员工查询</h1>
<label for="">请输入员工编号:</label>
<input type="text" id="keyword">
<button id="search">查询</button>
<p id="searchResult"></p>


<script type="text/javascript">
  document.getElementById("search").onclick = function() {
    var request = new XMLHttpRequest();
    request.open("GET","http://t.com/ajax/server.php?number=" + document.getElementById("keyword").value);
    request.send();
    request.onreadystatechange = function() {
      if (request.readyState === 4) {
        if(request.status == 200) {
          document.getElementById("searchResult").innerHtml = request.responseText;
        } else {
          alert("发生错误:request.status=" + request.status);
        }
      } else {
          alert("request.readyState=" + request.readyState);
      }
    }
  }
</script>
</body>
</html>

图片描述

阅读 2.5k
2 个回答

跨域了,控制台那么大2个红色error被你无视了吗?。
解决方式无非就是jsonp或者服务端配置允许跨域,自行搜索引擎吧

代码我复制了一次,新建了一个文件夹,然后重启了wampp,问题没有了,但是innerHtml无效,innerText可以,搞不懂啊

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进