【发布时间】:2018-06-10 19:17:49
【问题描述】:
我对 Laravel 5.3 CORS 有疑问。我在这个问题上搜索了很多,发现很多推荐 barryvdh 用于 CORS。然而这并没有奏效,我发现有人提到这可能是由于使用 tymondesigns jwt-auth 模块造成的。有人建议通过设置绕过它
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Authorization, Content-Type');
在 api.php 文件中。这会导致如下响应:
Failed to load https://example.com/api/v1/members/1: Method PUT is not allowed by Access-Control-Allow-Methods in preflight response.
为了解决这个问题,我添加了
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT');
到上面两行。然而,这给我留下了一个新问题:
PUT https://example.com/api/v1/members/1 403 (Forbidden)
Failed to load https://example.com/api/v1/members/1: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://example.com' is therefore not allowed access. The response had HTTP status code 403.
有什么建议可以解决这个问题吗?
【问题讨论】:
-
我很确定问题出在身份验证上。您绝对应该将 PUT 保留在 Allow-Method 标头中,并检查是否传递了正确的授权令牌。其他仅限身份验证的路由是否适合您?
-
它的身份验证部分在 api 和客户端共享域的 localhost 上运行良好。
标签: php laravel laravel-5 cors laravel-5.3