我使用 Laravel Lumen 作为 PHP REST 服务 API,与 Angular IO 应用程序集成。从 Postman 测试端点,运行良好。当使用暂存实时域并尝试从 Angular 应用程序调用端点到 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.phpclass 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),或者这绝对只是服务器端问题。谢谢你的建议。
1 回答
守着一只汪
TA贡献1872条经验 获得超3个赞
问题肯定出在 Laravel 方面,在 .htaccess 文件中,
# Redirect Trailing Slashes If Not A Folder..
如果它不是文件夹,它不接受端点末尾的“/”,即<URL/URI>/?Query_Parameters
应该是<URL/URI>?Query_Parameters
。
- 1 回答
- 0 关注
- 119 浏览
添加回答
举报
0/150
提交
取消