Angular IO 与 Laravel Lumen PHP API CORS 问题

Angular IO with Laravel Lumen PHP API CORS issue

我正在使用 Laravel Lumen 作为 PHP REST 服务 API,与 Angular IO 应用程序集成。从 Postman 测试端点,运行良好。当使用暂存实时域并尝试从 Angular 应用程序调用端点到 API 时,我收到 CORS 错误,尽管 headers 是从 Lumen 端使用中间件 class.

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://sub-domain.ext (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://api.sub-domain.ext. (Reason: CORS request did not succeed).

Access to XMLHttpRequest at 'http://api.sub-domain.ext' from origin 'http://sub-domain.ext' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

已使用如下中间件从 Lumen 设置 CORS headers:

//Http/Middleware/CorsMiddleware.php

class CorsMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return $next($request)
            ->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', 'GET, POST, DELETE, OPTIONS')
            ->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Authorization, X-Requested-With');        
    }
}

//bootstrap/app.php

 $app->middleware([
     App\Http\Middleware\CorsMiddleware::class
 ]);

直接从浏览器访问端点时,没有错误并且输出正确呈现。但是当从实时 domain/sub-domain(外部)拨打电话时,我收到了 CORS 错误。

不确定是否可以从 Angular 端设置任何内容(未使用 ExpressJS),或者它肯定只是服务器端问题。

感谢您的建议。

问题肯定出在 Laravel 方面,在 .htaccess 文件中,

# Redirect Trailing Slashes If Not A Folder..

如果它不是文件夹,它不接受端点末尾的“/”,即 <URL/URI>/?Query_Parameters 应该是 <URL/URI>?Query_Parameters