【问题标题】:angular send cookies from http request to php角度将cookie从http请求发送到php
【发布时间】:2016-11-08 05:25:46
【问题描述】:

所以这个问题问了好几次,但我还是想不通。我正在从 Angular 应用程序向 php 服务发送 http 请求。我需要访问我从浏览器设置的 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

我坚持了一天,但仍然无法解决这个问题。谢谢大家

【问题讨论】:

    标签: php angularjs cookies


    【解决方案1】:

    当你启用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");
    

    【讨论】:

    • 我可以使用多个域
    • @IT13122256RaawakaR.A.S.M 你将不得不添加多个规则,我已经更新了我的答案
    • 谢谢。我找到了使用此 header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}") 动态获取主机的解决方案
    • @IT13122256RaawakaR.A.S.M 虽然接受每个主机是不安全的,但我会创建一个白名单并检查来源是否在该列表中,否则拒绝它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多