请求没有'Access-Control-Allow-Origin'标头出现在请求的资源上只有POST请求

问题描述 投票:0回答:1

我很抱歉这个问题,但是我只用了2天才处理cakePHP 3.x,我看不出有什么问题。我正在从cakePHP应用程序请求POST请求,但我一直收到POST请求的错误(GET请求很好),我在middleware方法中使用下面的代码行,我还应该怎么办?这是激活CORS域的正确方法吗?

Application.php
  ->add(function($request, $response, $next) {
            return $next($request, $response)
                ->withHeader('Access-Control-Allow-Origin', '*')
                ->withHeader('Access-Control-Allow-Methods', '*')             
                ->withHeader('Access-Control-Allow-Credentials', 'true')
                ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With')
                ->withHeader('Access-Control-Allow-Headers', 'Content-Type')
                ->withHeader('Access-Control-Allow-Type', 'application/json');
        });
cakephp cakephp-3.0
1个回答
0
投票

我使用此工具来解决此问题,而不是将任何代码添加到应用程序文件:https://github.com/ozee31/cakephp-cors

© www.soinside.com 2019 - 2024. All rights reserved.