亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何解決訪問 Laravel 路由時出現 CORS 錯誤

如何解決訪問 Laravel 路由時出現 CORS 錯誤

PHP
呼啦一陣風 2023-06-24 17:52:24
我對 laravel 應用程序非常陌生。我正在嘗試開發一個使用 laravel 編寫的 API 的 Outlook Web 插件。這里的問題是,通過 Outlook 郵件訪問 API 時會產生 CORS 錯誤。錯誤 :Access to XMLHttpRequest at 'https://test.com/api/test' from origin 'https://localhost:44377' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.到目前為止我已經嘗試過:spatia/laravel-cors 模塊已安裝并嘗試在 bootstrap/app.php 中添加了以下內容:header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: *'); header('Access-Control-Allow-Headers: *');創建 CORS 類文件并添加為中間件最后還是報同樣的錯誤,我該怎么辦?編輯 :為什么它會自動將請求重定向到 https 而不是 http。哪里出了問題?請求 url 應該是http://test.com/api/test,而不是https://test.com/api/test提前致謝 !
查看完整描述

5 回答

?
弒天下

TA貢獻1818條經驗 獲得超8個贊

我正在使用 Laravel 8

檢查配置/cors.php

將路徑數組更改為 * ('paths' => ['*'])


查看完整回答
反對 回復 2023-06-24
?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

我也遇到了同樣的問題,通過中間件解決了

定義您的自定義中間件

//App\Http\Middleware;


public function handle($request, Closure $next)

{

? ? return $next($request)

? ? ? ? ->header('Access-Control-Allow-Origin', '*')

? ? ? ? ->header('Access-Control-Allow-Methods', '*')

? ? ? ? ->header('Access-Control-Allow-Credentials', true)

? ? ? ? ->header('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type,X-Token-Auth,Authorization')

? ? ? ? ->header('Accept', 'application/json');

}

不僅僅是注冊您的中間件,本地(針對特定路線)或全局。

查看完整回答
反對 回復 2023-06-24
?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

這對我來說工作:


php artisan make:middleware OwnCors

創建的中間件代碼:


<?php


namespace App\Http\Middleware;


use Closure;

use Illuminate\Http\Request;


class OwnCors

{

    /**

     * Handle an incoming request.

     *

     * @param \Illuminate\Http\Request $request

     * @param \Closure $next

     * @return mixed

     */

    public function handle(Request $request, Closure $next)

    {

        header("Access-Control-Allow-Origin: *");


        $headers = [

            'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE',

            'Access-Control-Allow-Headers' => 'Content-Type, X-Auth-Token, Origin, Authorization'

        ];

        if ($request->getMethod() == "OPTIONS") {

            return response('OK')

                ->withHeaders($headers);

        }


        $response = $next($request);

        foreach ($headers as $key => $value)

            $response->header($key, $value);

        return $response;

    }

}

將中間件添加到您的 App\Http\Kernel.php 中


    protected $middleware = [

         \App\Http\Middleware\OwnCors::class,

         // Others middlewares

];


查看完整回答
反對 回復 2023-06-24
?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

對于 Laravel 8


就我而言,我添加了需要訪問資源的源。


// config/cors.php


// add a path to the resource here if you want it accessible to external origins

// for example no need to explicitly tell allowed origins

// what origins should gain access to api/* routes

'paths' => ['api/*', 'sanctum/csrf-cookie'],

'allowed_methods' => ['*'],


// explicitly tell which origins needs access to the resource

'allowed_origins' => ['*', 'https://mywebsite.com', 'http://mywebsite.com'],


// or use regex pattern, helpful if you want to grant

// access to origins with certain pattern (i.e. an origin under a subdomain etc.)

'allowed_origins_patterns' => ['/https?:\/\/mywebsite\.com\/?\z/'],


// no changes made below

'allowed_headers' => ['*'],

'exposed_headers' => [],

'max_age' => 0,

'supports_credentials' => false,

php artisan optimize另外,如果您正在緩存配置,請不要忘記運行。


查看完整回答
反對 回復 2023-06-24
?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

如果您正在開發 Outlook 插件,您需要檢查的事項:

  • 檢查您是否已在 manifest.xml 中包含域名,在我的情況下,我需要包含https://test.com以包含在 <AppDomain> 標記中。

  • 您的域名應該有 ssl 證書。(即)outlook 允許您僅通過 https 請求。

查看完整回答
反對 回復 2023-06-24
  • 5 回答
  • 0 關注
  • 705 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號