javascript Laravel-CORS:您没有权限浏览此内容!

gg58donl  于 2023-04-28  发布在  Java
关注(0)|答案(1)|浏览(109)

已经采取的步骤:

  • 我已经用水果蛋糕/laravel-cors,也没有工作。
  • 目前,我尝试制作自己的中间件

所以我在localhost端口1220上运行我的前端,而我的Laravel端点在端口1240上运行。所以我的端点休息在1220,所以我可以模拟从不同的主机运行我的前端,而我的后端在另一个主机上。它们不应该在同一个主机中。
在我当前的laravel设置中,我创建了一个名为Cors的中间件

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class Cors
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
      $headers = [
        'Access-Control-Allow-Origin'      => '*',
        'Access-Control-Allow-Methods'     => 'POST, GET, OPTIONS, PUT, DELETE',
        'Access-Control-Allow-Credentials' => 'true',
        'Access-Control-Max-Age'           => '86400',
        // 'Access-Control-Allow-Headers'     => 'Content-Type, Authorization, X-Requested-With'
        'Access-Control-Allow-Headers'     => '*'
      ];

      if ($request->isMethod('OPTIONS'))
      {
          return response()->json('{"method":"OPTIONS"}', 200, $headers);
      }

      $response = $next($request);
      foreach($headers as $key => $value)
      {
          $response->header($key, $value);
      }

      return $response;
    }
}

在我的内核上。php我在web和API上的$middleware和$middlewareGroups上包含了Cors中间件

protected $middlewareGroups = [
        'web' => [
            EncryptCookies::class,
            AddQueuedCookiesToResponse::class,
            StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            ShareErrorsFromSession::class,
            VerifyCsrfToken::class,
            SubstituteBindings::class,
            // HandleCors::class,
            \App\Http\Middleware\Cors::class,
        ],

        'api' => [
            // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
            // 'throttle:api',
            // HandleCors::class,
            SubstituteBindings::class,
            AuthenticateSession::class,
            StartSession::class,
            \App\Http\Middleware\Cors::class,
        ],

    ];

protected $routeMiddleware = [
        'sessions' => StartSession::class,
        'auth' => Authenticate::class,
        'auth.basic' => AuthenticateWithBasicAuth::class,
        'cache.headers' => SetCacheHeaders::class,
        'can' => Authorize::class,
        'guest' => RedirectIfAuthenticated::class,
        'password.confirm' => RequirePassword::class,
        'signed' => ValidateSignature::class,
        'throttle' => ThrottleRequests::class,
        'verified' => EnsureEmailIsVerified::class,
        'cors' => \App\Http\Middleware\Cors::class,
    ];

但是添加后我仍然得到这个错误
抱歉,您没有权限访问该资源!Apache/2.4.41(Ubuntu)服务器地址:localhost端口1220

lyr7nygr

lyr7nygr1#

使用npm start服务(或使用npm服务)。前端和后端应分别托管。它不能全部托管在Apache中。

相关问题