我使用 Laravel Lumen 作為 PHP REST 服務(wù) API,與 Angular IO 應(yīng)用程序集成。從 Postman 測(cè)試端點(diǎn),運(yùn)行良好。當(dāng)使用暫存實(shí)時(shí)域并嘗試從 Angular 應(yīng)用程序調(diào)用端點(diǎn)到 API 時(shí),我收到 CORS 錯(cuò)誤,盡管標(biāo)頭是從帶有中間件類的 Lumen 端設(shè)置的。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 標(biāo)頭是使用中間件從 Lumen 設(shè)置的,如下所示://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 ]);當(dāng)直接從瀏覽器訪問(wèn)端點(diǎn)時(shí),沒有錯(cuò)誤并且輸出正確呈現(xiàn)。但是當(dāng)從實(shí)時(shí)域/子域(外部)進(jìn)行調(diào)用時(shí),我收到了 CORS 錯(cuò)誤。不確定是否可以從 Angular 端設(shè)置任何內(nèi)容(未使用 ExpressJS),或者這絕對(duì)只是服務(wù)器端問(wèn)題。謝謝你的建議。
帶有 Laravel Lumen PHP API CORS 問(wèn)題的 Angular IO
三國(guó)紛爭(zhēng)
2022-12-11 19:14:48