Laravel API CORS模式不起作用 [英] Laravel API CORS mode not working

查看:60
本文介绍了Laravel API CORS模式不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了很多研究,并实现了以下中间件来解决我不断遇到的CORS错误...

I did a lot of research and I implemented the following middleware to get around the CORS error I keep getting...

Middleware/Cors.php

Middleware/Cors.php

<?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,PATCH,DELETE,OPTIONS')
        ->header('Access-Control-Allow-Headers', 'content-type');
    }
}

?>

Kernel.php

Kernel.php

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'cors' => App\Http\Middleware\Cors,
];

Routes.php

Routes.php

Route::post('/content', array('middleware' => 'cors', 'uses' => 'Test@save'));

这似乎配置正确.但是当我使用提取API发出请求时...

This seems to be configured properly. But when I make the request using fetch API...

fetch('http://laravel.dev/content', {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'Access-Control-Allow-Origin':'*'
        },
        body: JSON.stringify({

        })
    })
    //.then((response) => response.json())
    .then((response) => response.text()) 
    .then((text) => {
        console.log(text);
    });

我仍然收到此错误...

I still get this error...

Fetch API cannot load http://laravel.dev/content. 
Response to preflight request doesn't pass access 
control check: No 'Access-Control-Allow-Origin' header 
is present on the requested resource.

我尝试在获取响应中添加no-cors模式,但工作正常,响应不透明,但是我需要使用cors模式来获取正确的请求.

I tried adding no-cors mode to my fetch response, and it worked, I got an opaque response, but I need cors mode to get a proper request.

可能值得一提的是,我的App和Laravel restAPI位于XAMPP上的同一htdocs文件夹中.我不确定这是否是问题.

It might be worthwhile to note that my App and my Laravel restAPI are located in the same htdocs folder on XAMPP. I'm not sure if this is an issue.

我也尝试将标题添加到Routers.php文件的顶部,但仍然出现相同的错误.甚至可以使用laravel配置restAPI吗?

I also tried adding the headers at the top of my Routers.php file but I still get the same error. Can I even configure a restAPI using laravel?

推荐答案

尝试删除

->header('Access-Control-Allow-Headers', 'content-type');

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,PATCH,DELETE,OPTIONS');

       }
  }

 ?>

对我有用.希望对您有帮助

It worked for me.I hope it helpfull for you

这篇关于Laravel API CORS模式不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆