我正在關注一個書籍示例,因此代碼非常簡單。僅在Chrome中出錯:XMLHttpRequest無法加載文件URL請求的資源中沒有「Access-Control-Allow-Origin」標頭
這是代碼:
jQuery.get("ajax_search_results.php",
{ s:search_query },
write_results_to_page, "html");
而且代碼ajax_search_results.php有:
<div id="ajax_search_results">
<ul>
<li><a href="#">First result</a></li>
<li><a href="#">Second result</a></li>
<li><a href="#">Third result</a></li>
<li><a href="#">More...</a></li>
</ul>
</div>
可正常工作的Firefox瀏覽器。 但在Chrome中,我可以看到在JS控制檯中的錯誤以下消息:
XMLHttpRequest cannot load
file:///C:/xampp/xampp/htdocs/snk/ajax_search_results.php?s=keyword.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'null' is therefore not allowed access.
能否請你幫忙找出問題?
通過Web服務器打開文件,例如'http:// localhost/someFile.html'。 Chrome默認情況下不允許針對'file:///'URL的AJAX請求,您的PHP腳本也不會執行 – Phil
您是否將其作爲文件打開?打開它在服務器上,這意味着地址就像http:// .... – Andrew