【发布时间】:2018-12-24 07:16:16
【问题描述】:
在我的应用程序中,我正在构建一个前端网站(基于 React),并且我使用 PHP 作为我的后端。 我不断收到错误消息:
jquery.js:9600 加载失败http://localhost:81/: 的值 响应中的“Access-Control-Allow-Credentials”标头是“” 当请求的凭据模式为“包含”时,必须为“真”。 因此,Origin 'http://localhost:3000' 不允许访问。这 XMLHttpRequest 发起的请求的凭证模式是 由 withCredentials 属性控制。
我的 PHP 标头是:
header('Access-Control-Allow-Origin: http://localhost:3000');
// header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
header('Access-Control-Allow-Headers');
header('Access-Contol-Allow-Credentials: true');
我的前端请求如下所示:
$.post(
{
async: false,
type: "POST",
url:'http://localhost:81',
data:
{
querytype: "dataRequest"
},
crossDomain : true,
xhrFields: {
withCredentials: true
},
complete: this.completed,
success: this.reqsuccess,
error: this.reqError
});
【问题讨论】: