' http:// localhost:3000' CORS政策已阻止

发布于 2025-02-12 17:48:20 字数 1617 浏览 0 评论 0原文

API:数据库的Laravel和MongoDB 正面:nuxtjs和vuetify

我有自己的API。当我在Postman上测试路线时,我有CORS错误,但是我在文件路由API.php中使用此代码解决了。

header('Access-Control-Allow-Origin:  *');
header('Access-Control-Allow-Methods:  POST, GET, OPTIONS, PUT, PATCH, DELETE');

也许有一个更好的文件可以放置此代码。但是我和邮递员一起工作。如果我需要移动此代码,请告诉我。

现在,我的实际问题,在我的正面网站上,当我想注册一个新用户时,

Blocage d'une requête multiorigine (Cross-Origin Request) : la politique « Same Origin » ne permet pas de consulter la ressource distante située sur http://127.0.0.1:8000/api/register. Raison : l'en-tête « content-type » n'est pas autorisé d'après l'en-tête « Access-Control-Allow-Headers » de la réponse de pré-vérification des requêtes CORS.

Access to XMLHttpRequest at 'http://127.0.0.1:8000/api/register' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.

我的文件中有一个错误:config/cors.php,我有:

'paths' => ['api/*', 'sanctum/csrf-cookie'],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => false,

];

感谢您的帮助

编辑解决方案,

我找到了答案。我只是补充: 访问控制 - 允许头:api.php中的“ content-type”

header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, PATCH, DELETE'); header('Access-Control-Allow-Headers: Content-Type') ;

API : Laravel and MongoDB for database
Front : NuxtJs and Vuetify

I have my own API. When I test my routes on Postman, I had cors error, but I solved with this code in file routes api.php.

header('Access-Control-Allow-Origin:  *');
header('Access-Control-Allow-Methods:  POST, GET, OPTIONS, PUT, PATCH, DELETE');

Maybe, there is a better file to put this code. But I work with Postman. If I need to move this code, tell me please.

Now, my actual problem, on my front site, when i want sign up a new user, I have this error

Blocage d'une requête multiorigine (Cross-Origin Request) : la politique « Same Origin » ne permet pas de consulter la ressource distante située sur http://127.0.0.1:8000/api/register. Raison : l'en-tête « content-type » n'est pas autorisé d'après l'en-tête « Access-Control-Allow-Headers » de la réponse de pré-vérification des requêtes CORS.

Access to XMLHttpRequest at 'http://127.0.0.1:8000/api/register' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.

In my file : config/cors.php I have :

'paths' => ['api/*', 'sanctum/csrf-cookie'],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => false,

];

Thanks for your help

Edit Resolved

I have found the answer. I just add :
Access-Control-Allow-Headers: 'Content-Type' in api.php

header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, PATCH, DELETE'); header('Access-Control-Allow-Headers: Content-Type') ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文