• <legend id='3H0Yz'><style id='3H0Yz'><dir id='3H0Yz'><q id='3H0Yz'></q></dir></style></legend><tfoot id='3H0Yz'></tfoot>

      <bdo id='3H0Yz'></bdo><ul id='3H0Yz'></ul>

      <small id='3H0Yz'></small><noframes id='3H0Yz'>

        <i id='3H0Yz'><tr id='3H0Yz'><dt id='3H0Yz'><q id='3H0Yz'><span id='3H0Yz'><b id='3H0Yz'><form id='3H0Yz'><ins id='3H0Yz'></ins><ul id='3H0Yz'></ul><sub id='3H0Yz'></sub></form><legend id='3H0Yz'></legend><bdo id='3H0Yz'><pre id='3H0Yz'><center id='3H0Yz'></center></pre></bdo></b><th id='3H0Yz'></th></span></q></dt></tr></i><div id='3H0Yz'><tfoot id='3H0Yz'></tfoot><dl id='3H0Yz'><fieldset id='3H0Yz'></fieldset></dl></div>

        React 应用程序和 Laravel API 的 CORS 问题

        CORS Issue with React app and Laravel API(React 应用程序和 Laravel API 的 CORS 问题)

          <i id='kxjLf'><tr id='kxjLf'><dt id='kxjLf'><q id='kxjLf'><span id='kxjLf'><b id='kxjLf'><form id='kxjLf'><ins id='kxjLf'></ins><ul id='kxjLf'></ul><sub id='kxjLf'></sub></form><legend id='kxjLf'></legend><bdo id='kxjLf'><pre id='kxjLf'><center id='kxjLf'></center></pre></bdo></b><th id='kxjLf'></th></span></q></dt></tr></i><div id='kxjLf'><tfoot id='kxjLf'></tfoot><dl id='kxjLf'><fieldset id='kxjLf'></fieldset></dl></div>

          1. <tfoot id='kxjLf'></tfoot>
            • <bdo id='kxjLf'></bdo><ul id='kxjLf'></ul>

                <legend id='kxjLf'><style id='kxjLf'><dir id='kxjLf'><q id='kxjLf'></q></dir></style></legend>
              • <small id='kxjLf'></small><noframes id='kxjLf'>

                  <tbody id='kxjLf'></tbody>
                • 本文介绍了React 应用程序和 Laravel API 的 CORS 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 React 应用程序,它位于

                  解决方案

                  下面的解决方案应该可以解决 Laravel 中与 CORS 相关的问题.

                  第一步:创建一个新的中间件

                  ‘Php artisan make:middleware cors’

                  第 2 步:

                  将下面的内容放在创建的中间替换句柄方法

                   公共函数句柄($request, Closure $next) {返回 $next($request)->header('Access-Control-Allow-Origin', '*')->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')->header('Access-Control-Allow-Headers','Origin, Content-Type, Accept, Authorization, X-Request-With')->header('Access-Control-Allow-Credentials','true');}

                  第 3 步:

                  然后转到 Kernel.php 文件并将其添加到应用程序的全局 HTTP 中间件堆栈下.

                  附言仅添加了带有注释的最后一行,其他行之前存在.

                  受保护的 $middleware = [IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode::class,IlluminateFoundationHttpMiddlewareValidatePostSize::class,AppHttpMiddlewareTrimStrings::class,IlluminateFoundationHttpMiddlewareConvertEmptyStringsToNull::class,AppHttpMiddlewareTrustProxies::class,AppHttpMiddlewareCors::class,//cors 添加在这里];

                  享受吧!

                  I have a React app which is at http://localhost:3000/ and Laravel API is at http://localhost/blog/public/api/
                  I get the following error

                  Access to fetch at 'http://localhost/blog/public/api/auth/signin' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

                  Here are the response headers :-

                  I tried via htaccess, https://packagist.org/packages/barryvdh/laravel-cors

                  解决方案

                  The below solution should fix the CORS related issue in Laravel.

                  Step1: Create a new middleware

                  ‘Php artisan make:middleware cors’
                  

                  Step 2:

                  Put the below in the created middle to replace the handle method

                      public function handle($request, Closure $next) {
                     
                      return $next($request)
                        ->header('Access-Control-Allow-Origin', '*')
                        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
                        ->header('Access-Control-Allow-Headers',' Origin, Content-Type, Accept, Authorization, X-Request-With')
                        ->header('Access-Control-Allow-Credentials',' true');
                  }
                  

                  Step 3:

                  Then go to Kernel.php file and add this under the The application's global HTTP middleware stack.

                  p.s. Only the last line with the comment was added, the other other lines exist before.

                  protected $middleware = [
                  IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode::class,
                  IlluminateFoundationHttpMiddlewareValidatePostSize::class,
                  AppHttpMiddlewareTrimStrings::class,
                  IlluminateFoundationHttpMiddlewareConvertEmptyStringsToNull::class,
                  AppHttpMiddlewareTrustProxies::class,
                  AppHttpMiddlewareCors::class,//cors added here 
                   ];
                  

                  Enjoy!

                  这篇关于React 应用程序和 Laravel API 的 CORS 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How do I parse XML containing custom namespaces using SimpleXML?(如何使用 SimpleXML 解析包含自定义命名空间的 XML?)
                  SimpleXML SOAP response Namespace issues(SimpleXML SOAP 响应命名空间问题)
                  Problems with PHP namespaces and built-in classes, how to fix?(PHP 命名空间和内置类的问题,如何解决?)
                  Use php namespace inside function(在函数内部使用 php 命名空间)
                  unexpected #39;use#39; (T_USE) when trying to use composer(尝试使用作曲家时意外的“使用(T_USE))
                  PHP adding custom namespace using autoloader from composer(PHP使用来自作曲家的自动加载器添加自定义命名空间)

                  <small id='7XWCv'></small><noframes id='7XWCv'>

                  <legend id='7XWCv'><style id='7XWCv'><dir id='7XWCv'><q id='7XWCv'></q></dir></style></legend>
                  <i id='7XWCv'><tr id='7XWCv'><dt id='7XWCv'><q id='7XWCv'><span id='7XWCv'><b id='7XWCv'><form id='7XWCv'><ins id='7XWCv'></ins><ul id='7XWCv'></ul><sub id='7XWCv'></sub></form><legend id='7XWCv'></legend><bdo id='7XWCv'><pre id='7XWCv'><center id='7XWCv'></center></pre></bdo></b><th id='7XWCv'></th></span></q></dt></tr></i><div id='7XWCv'><tfoot id='7XWCv'></tfoot><dl id='7XWCv'><fieldset id='7XWCv'></fieldset></dl></div>
                    1. <tfoot id='7XWCv'></tfoot>
                        <bdo id='7XWCv'></bdo><ul id='7XWCv'></ul>

                            <tbody id='7XWCv'></tbody>