CORS使用PHP阻止AJAX [重复]

我试图运行一个使用PHP文件的简单AJAX程序。

h2.html

<html>
<head>
  <script>
    function showHint(str) {
      if (str.length == 0) {
        document.getElementById("txtHint").innerHTML = "";
        return;
      } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function () {
          if (this.readyState == 4 && this.status == 200) {
            document.getElementById("txtHint").innerHTML = this.responseText;
          }
        };
        xmlhttp.withCredentials = true; 
        xmlhttp.open("GET", "p2.php?q=" + str, true);
        xmlhttp.send();
      }
    }
  </script>
</head>

<body>
  <p><b>Start typing a name in the input field below:</b></p>
  <form action="">
    <label for="fname">First name:</label>
    <input type="text" id="fname" name="fname" onkeyup="showHint(this.value)">
  </form>
  <p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>

p2.php

<?php
header('Access-Control-Allow-Origin: *');
$a[] = "Anna";
$a[] = "Anna2";
$a[] = "Anna3";

$q = $_REQUEST["q"];

$hint = "";

if ($q !== "") {
    $q = strtolower($q);
    $len = strlen($q);
    foreach ($a as $name) {
        if (stristr($q, substr($name, 0, $len))) {
            if ($hint === "") {
                $hint = $name;
            } else {
                $hint .= ", $name";
            }
        }
    }
}

echo $hint === "" ? "no suggestion" : $hint;
?> 

在Chrome中,出现以下错误:

从以下位置访问XMLHttpRequest:“ file:/// E:/mypath/p2.php?q = sd”   原点“空”已被CORS政策阻止:跨原点请求   仅支持协议方案:http,data,chrome,   chrome-extension,https。

在Firefox中,我得到以下信息:

跨域请求被阻止:同源策略禁止阅读   文件:/// E:/mypath/p2.php?q = aad中的远程资源。 (原因:CORS   请求不是http)。

我尝试使用互联网上不同的代码示例,但都无法正常工作,我尝试过使用PHP文件的不同标头,我尝试过在HTML文件中添加“ xmlhttp.withCredentials = true”,在网上搜索了几个小时,但没有任何反应。