【问题标题】:NuxtJS Proxy to avoid CORSNuxtJS 代理以避免 CORS
【发布时间】:2021-05-09 02:17:04
【问题描述】:
我有两个带有 baseURL 的 axios 实例“https://localhost:8082”和“https://localhost:8083”,我的 nuxtjs SSR 服务器在“https://localhost:8081”上运行。我尝试了很多代理和 CORS,但对我没有任何帮助。有谁知道,如何成功配置 @nuxtjs/proxy 为我的项目工作?
重要的是它必须与 SSR 一起使用。
谢谢。
【问题讨论】:
标签:
typescript
proxy
axios
server-side-rendering
nuxtjs
【解决方案1】:
尝试在您的 API 上添加 CORS 政策。
如果使用 Lumen,请创建一个名为 CorsMiddleware.php
的中间件
<?php namespace App\Http\Middleware;
use Closure;
class CorsMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$headers = [
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE',
'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Max-Age' => '86400',
'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With'
];
if ($request->isMethod('OPTIONS'))
{
return response()->json('{"method":"OPTIONS"}', 200, $headers);
}
$response = $next($request);
foreach($headers as $key => $value)
{
$response->header($key, $value);
}
return $response;
}}