我们正在一个项目上使用 vue-bulma-admin
脚手架,它已经使用了 webpack,我们似乎无法对我们的 API 执行任何 POST/PUT 请求(它已经设置了访问权限-cross-allow-origin header 到 *),只有 GET 请求有效。从使用我们的 API 的不同前端,我们没有这个问题,只有这个特定的前端,它给出了这个常见错误:
"No 'Access-Control-Allow-Origin' header is present on the requested resource."
Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested
resource.
我们已经尝试在 axios/vue-resource
上设置自定义 header ,但没有成功。正如我所说,通过 Postman/不同的前端或使用 CORS chrome 扩展使用相同的路由,一切正常。
有解决这个问题的想法吗?
我在 Laravel API 中的中间件 Cors。
<?php
namespace App\Http\Middleware;
use Closure;
class Cors
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
}
}
最佳答案
我使用软件包 https://github.com/barryvdh/laravel-cors 解决了我的问题
关于laravel - vue使用CORS的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46058665/