大家好!
在这里检查了很多类似问题的问题,但它们完全不同,没有任何作用。 问题作者无法控制的第三方资源存在问题,在我的情况下,我强制从服务器发送 header ,但什么也没收到: No 'Access-Control-Allow-Origin' header is present on the requested resource error
我在 CORS 请求方面遇到问题。这是我的 JS(使用 jQuery):
$.ajax({
type: "POST",
url: "http://domain.com/module/api/storeData.php",
data: 'data='+results,
dataType: "html",
async: false,
crossDomain: true,
success: function(msg){
if(msg=="Ok")
{
$("#resultsPane").append ("Results successfully sent.");
} else {
$("#resultsPane").append (msg);
}
},
error: function (xhr, status, errorT)
{
$("#resultsPane").append ("AJAX error: "+status+": "+errorT);
}
});
PHP 的开始:
<?php
header ("Access-Control-Allow-Origin", "*");
但结果是(在控制台中):
jquery.js:10254 XMLHttpRequest cannot load http://domain.com/module/api/storeData.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.
响应 header :
Connection:Keep-Alive
Content-Length:2
Content-Type:text/html
Date:Thu, 13 Oct 2016 13:19:23 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.7 (Ubuntu)
X-Powered-By:PHP/5.5.9-1ubuntu4.17
浏览器中的 AJAX 错误:
AJAX error: error: NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://domain.com/module/api/storeData.php'.
有人知道会出什么问题吗?预先感谢您!
最佳答案
与其他各种函数和方法不同,您希望将项目设置为键,值
作为单独的参数,header才不是。相反,它期望您要设置的标题行的整个字符串是第一个参数。
即
header ("Access-Control-Allow-Origin: *");
请注意,您应该考虑指定严格允许的特定主机,而不是出于安全原因允许任何访问。
关于javascript - Access-Control-Allow-Origin 不从 PHP 发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40022507/