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 关注
- 602 浏览
添加回答
举报