• <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 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                  <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>