【问题标题】:Php / AngularJs / Ionic Request header field Authorization is not allowedphp / AngularJs / Ionic Request 头域 Authorization is not allowed
【发布时间】:2023-03-11 16:51:01
【问题描述】:

首先我想说我尝试了所有stackoverflow解决方案,没有人适合我:(

在我的服务器配置中我有这个

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
// respond to preflights
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
// return only the headers and not the content
// only allow CORS if we're doing a GET - i.e. no saving for now.
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']) && $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] == 'GET') {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
}
exit;
}

在我的应用程序中,我一直在配置 $httpProvider。

.config(['$httpProvider',
    function($httpProvider) {
        function tokenInjector($localStorage) {
             return {
                 request: function(config) {
                     if ($localStorage.token) {
                         config.headers.authorization = 'Basic ' + $localStorage.token;
                     } else {
                         delete config.headers.authorization;
                     }

                     return config;
                 }
             };
        }
        $httpProvider.interceptors.push(tokenInjector);

    }
])

.config(function ($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    $httpProvider.defaults.withCredentials = false;
    delete $httpProvider.defaults.headers.common["X-Requested-With"];
    $httpProvider.defaults.headers.common["Accept"] = "application/json";
    $httpProvider.defaults.headers.common["Content-Type"] = "application/json";
});

服务调用(带有令牌的标头已在 $httpProvider.interceptors 中设置)

$http.get('http://myurl')

问题是......我不知道为什么,但在我的手机(Android 或 iOS)上工作正常,但......在我的网络浏览器中却不行。我在 chrome、mozilla 和 safari 中尝试过。 如果我安装 Chrome 插件 CORS 工作正常,但不总是返回该错误

加载资源失败:Access-Control-Allow-Headers 不允许请求头字段授权。

XMLHttpRequest 无法加载 http://myurl。要求 头字段 Authorization is not allowed by 访问控制允许标头。

如何在没有 CORS 插件的情况下在浏览器中解决这个问题?

感谢所有愿意帮助的人:)

【问题讨论】:

    标签: php angularjs apache ionic-framework cors


    【解决方案1】:

    我认为这不是服务器问题,请检查此链接:

    http://blog.ionic.io/handling-cors-issues-in-ionic/

    然后这个链接:

    api calling error in ionic-framework

    【讨论】:

    • 感谢您的快速回复 :) 该解决方案效果很好,您节省了我的时间
    猜你喜欢
    • 2018-06-14
    • 2016-08-20
    • 1970-01-01
    • 2018-05-01
    • 2018-02-28
    • 2017-10-23
    • 2017-08-27
    • 1970-01-01
    • 2017-10-12
    相关资源
    最近更新 更多