第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

React 應(yīng)用程序和 Laravel API 的 CORS 問題

React 應(yīng)用程序和 Laravel API 的 CORS 問題

PHP
ibeautiful 2021-06-10 22:25:01
我有一個位于http://localhost:3000/的 React 應(yīng)用程序, 而 Laravel API 位于http://localhost/blog/public/api/我收到以下錯誤從源 ' http://localhost:3000 '獲取在 ' http://localhost/blog/public/api/auth/signin '的訪問已被 CORS 策略阻止:沒有 'Access-Control-Allow-Origin' 標(biāo)頭存在于請求的資源上。如果不透明響應(yīng)滿足您的需求,請將請求的模式設(shè)置為“no-cors”以在禁用 CORS 的情況下獲取資源。這是響應(yīng)標(biāo)頭:-我嘗試通過 htaccess,https: //packagist.org/packages/barryvdh/laravel-cors
查看完整描述

3 回答

?
慕妹3242003

TA貢獻(xiàn)1824條經(jīng)驗 獲得超6個贊

下面的解決方案應(yīng)該解決 Laravel 中與 CORS 相關(guān)的問題。


Step1:創(chuàng)建一個新的中間件


‘Php artisan make:middleware cors’

第2步:


把下面的放在創(chuàng)建的中間替換handle方法


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

   

    return $next($request)

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

      ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')

      ->header('Access-Control-Allow-Headers',' Origin, Content-Type, Accept, Authorization, X-Request-With')

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

}

第 3 步:


然后轉(zhuǎn)到 Kernel.php 文件并將其添加到應(yīng)用程序的全局 HTTP 中間件堆棧下。


ps 只添加了帶有注釋的最后一行,其他行之前存在。


protected $middleware = [

\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,

\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,

\App\Http\Middleware\TrimStrings::class,

\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,

\App\Http\Middleware\TrustProxies::class,

\App\Http\Middleware\Cors::class,//cors added here 

 ];

享受!


查看完整回答
反對 回復(fù) 2021-06-19
  • 3 回答
  • 0 關(guān)注
  • 206 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號