在Google App Engine灵活环境PHP(Laravel)上启用CORS

在Google App Engine灵活环境PHP(Laravel)上启用CORS,php,google-app-engine,Php,Google App Engine,0 我面临着使CORS支持使用Google App Engine和灵活环境托管的PHP(laravel)应用程序的问题 使用axios库的每个AJAX请求都会导致以下错误 访问位于“”的XMLHttpRequesthttps://api.[something]。服务/请求“来自源”http://localhost:8080'已被CORS策略阻止:请求的资源上不存在'Access Control Allow Origin'标头。您很可能需要添加: header('Access-Control-Al

0

我面临着使CORS支持使用Google App Engine和灵活环境托管的PHP(laravel)应用程序的问题

使用axios库的每个AJAX请求都会导致以下错误


访问位于“”的XMLHttpRequesthttps://api.[something]。服务/请求“来自源”http://localhost:8080'已被CORS策略阻止:请求的资源上不存在'Access Control Allow Origin'标头。

您很可能需要添加:

header('Access-Control-Allow-Origin: *');
上面的PHP代码表明,通配符
*
将允许每个人向web应用程序发出Ajax请求

header('Access-Control-Allow-Origin: http://example.com');
上面的PHP代码表明
example.com
有权向web应用程序发出跨域请求

header('Access-Control-Allow-Origin: http://example.com');
此外,我建议您查看一下,使用Laravel中间件配置发送跨源资源共享头

另见:

这是app.yml的一部分

使用水果饼\Cors

和cors配置im使用默认值

'paths' => ['api/*'],

    /*
    * Matches the request method. `['*']` allows all methods.
    */
    'allowed_methods' => ['*'],

    /*
     * Matches the request origin. `['*']` allows all origins. Wildcards can be used, eg `*.mydomain.com`
     */
    'allowed_origins' => ['*'],

    /*
     * Patterns that can be used with `preg_match` to match the origin.
     */
    'allowed_origins_patterns' => [],

    /*
     * Sets the Access-Control-Allow-Headers response header. `['*']` allows all headers.
     */
    'allowed_headers' => ['*'],

感谢您的支持

Im尝试使用此PHP Laravel包,并在index.PHP中添加头,但无法发送请求ajax.Im在其他服务器linux中使用它工作正常。但是deloy的GAE flex不起作用了我使用了nginx-app.conf,但也遇到了同样的问题。你能帮我吗?我想,如果你能发布一个最小的可复制的示例代码来查看你的配置/你是如何提出请求的,并且能够进一步帮助你,那就太好了。您的另一台Linux服务器托管在哪里?还有GCP?您的应用程序引擎路径端点是否以代码中定义的
api/
开头?您是否尝试过在应用程序的代码中添加
访问控制允许源代码
标题?另请参见以下内容。如果这不起作用,请告诉我将
\Fruitcake\Cors\HandleCors::class,
移动到
$middleware
列表顶部是否会产生任何影响,并确保所述步骤正确。
'paths' => ['api/*'],

    /*
    * Matches the request method. `['*']` allows all methods.
    */
    'allowed_methods' => ['*'],

    /*
     * Matches the request origin. `['*']` allows all origins. Wildcards can be used, eg `*.mydomain.com`
     */
    'allowed_origins' => ['*'],

    /*
     * Patterns that can be used with `preg_match` to match the origin.
     */
    'allowed_origins_patterns' => [],

    /*
     * Sets the Access-Control-Allow-Headers response header. `['*']` allows all headers.
     */
    'allowed_headers' => ['*'],