Angular IO with Laravel Lumen PHP API CORS问题

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

我正在使用Laravel Lumen作为与Angular IO应用程序集成的PHP REST服务API。从Postman测试端点,效果很好。当使用分段实时域并尝试从Angular Application调用端点到API时,我收到CORS错误,尽管标头是通过Lumen端使用中间件类设置的。

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.

[CORS标头是使用以下中间件从Lumen设置的:

//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
 ]);

直接从浏览器中命中端点时,没有错误,并且输出呈现正确。但是,当从活动域/子域(外部)进行呼叫时,我收到了CORS错误。

不确定是否可以从Angular端设置任何内容(不使用ExpressJS,或者绝对是服务器端的问题。

谢谢您的建议。

php angular api cors lumen
1个回答
0
投票

这个问题肯定是来自Laravel方面的.htaccess文件中的,

# Redirect Trailing Slashes If Not A Folder..

如果不是文件夹,则它在端点末尾不接受'/',即<URL/URI>/?Query_Parameters应该为<URL/URI>?Query_Parameters

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