Angular将cookie从HTTP请求发送到php


angular send cookies from http request to php

所以这个问题问了好几次,但我仍然不能弄清楚。我正在发送HTTP请求从angular应用程序到PHP服务。我需要从浏览器访问我设置的cookie。我知道要从php服务访问cookie,我必须在http请求中设置withCredentials。但是再次引发通配符错误。

这里是HTTP请求

$http({
    url : $scope.urlDomain+ "setting/getAll?skip=0&take=10&orderby=asc",
    method :"POST",
    headers : {
        securityToken : "1124"
    },
    withCredentials: true 
})

我已经在PHP文件中设置了头文件

header("Access-Control-Allow-Origin: * ");
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Methods: PUT,POST, GET, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: origin, x-requested-with, content-type, securityToken");

这是错误

XMLHttpRequest cannot load http://localhost/services/getAll?skip=0&take=10&orderby=asc. Response to preflight request doesn't pass access control check: A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost' is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute

我坚持了一天,仍然没有弄明白。谢谢大家

启用withCredentials时不能使用header("Access-Control-Allow-Origin: * ");,必须这样指定域:

header("Access-Control-Allow-Origin: 127.0.0.1");
header("Access-Control-Allow-Origin: domain2");
header("Access-Control-Allow-Origin: domain3");