gom*_*ena 4 php outlook outlook-addin cors laravel
我对 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.
Run Code Online (Sandbox Code Playgroud)
到目前为止我已经尝试过:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: *');
header('Access-Control-Allow-Headers: *');
Run Code Online (Sandbox Code Playgroud)
最后还是报同样的错误,我该怎么办?
编辑 :
为什么它会自动将请求重定向到 https 而不是 http。哪里出了问题?请求 url 应该是http://test.com/api/test,而不是https://test.com/api/test
提前致谢 !
我也遇到了同样的问题,通过中间件解决了
//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');
}
Run Code Online (Sandbox Code Playgroud)
不仅仅是注册您的中间件,本地(针对特定路线)或全局。
注意!一些旧浏览器不支持“*”逻辑