5 回答

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');
}
不僅僅是注冊您的中間件,本地(針對特定路線)或全局。

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

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另外,如果您正在緩存配置,請不要忘記運行。

TA貢獻1776條經驗 獲得超12個贊
如果您正在開發 Outlook 插件,您需要檢查的事項:
檢查您是否已在 manifest.xml 中包含域名,在我的情況下,我需要包含https://test.com以包含在 <AppDomain> 標記中。
您的域名應該有 ssl 證書。(即)outlook 允許您僅通過 https 請求。
- 5 回答
- 0 關注
- 705 瀏覽
添加回答
舉報