遇到的程序跨域问题,分享一下解决方法
这里报错
XMLHttpRequest cannot load http://*****. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' istherefore not allowed access.
第一步,创建中间件
在 app/Http/Middleware/ 目录下创建AccessControlAllowOrigin.php,把下方完全Ctrl+c 、Ctrl+v即可
<?php namespace AppHttpMiddleware; use Closure; use IlluminateSupportFacadesAuth; class AccessControlAllowOrigin { /** * * Handle an incoming request. * * @param IlluminateHttpRequest $request * @param Closure $next * @return mixed */ public function handle($request, Closure $next) { header('Access-Control-Allow-Origin: *'); header("Access-Control-Allow-Credentials: true"); header("Access-Control-Allow-Methods: *"); header("Access-Control-Allow-Headers: Content-Type,Access-Token"); header("Access-Control-Expose-Headers: *"); return $next($request); } }
第二步,设置保护中间件
打开 app/Http/Kernel.php
在 protected
$middleware数组中加入
AppHttpMiddlewareAccessControlAllowOrigin::
class
,
/** * The application's global HTTP middleware stack. * * These middleware are run during every request to your application. * * @var array */ protected $middleware = [ IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode::class, IlluminateFoundationHttpMiddlewareValidatePostSize::class, AppHttpMiddlewareTrimStrings::class, IlluminateFoundationHttpMiddlewareConvertEmptyStringsToNull::class, AppHttpMiddlewareAccessControlAllowOrigin::class, ];
在 protected
$middlewareGroups数组的 api 加入 你自定义路由的名称,我这里是 diyapp
protected $middlewareGroups = [ 'web' => [ AppHttpMiddlewareEncryptCookies::class, IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class, IlluminateSessionMiddlewareStartSession::class, // IlluminateSessionMiddlewareAuthenticateSession::class, IlluminateViewMiddlewareShareErrorsFromSession::class, AppHttpMiddlewareVerifyCsrfToken::class, IlluminateRoutingMiddlewareSubstituteBindings::class, ], 'api' => [ 'throttle:60,1', IlluminateRoutingMiddlewareSubstituteBindings::class, 'diyapp', ], ];
在 protected
$routeMiddleware 数组中加入
'diyapp'
=> AppHttpMiddlewareAccessControlAllowOrigin::
class
,
protected $routeMiddleware = [ 'auth' => AppHttpMiddlewareAuthenticate::class, 'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class, 'bindings' => IlluminateRoutingMiddlewareSubstituteBindings::class, 'cache.headers' => IlluminateHttpMiddlewareSetCacheHeaders::class, 'can' => IlluminateAuthMiddlewareAuthorize::class, 'guest' => AppHttpMiddlewareRedirectIfAuthenticated::class, 'password.confirm' => IlluminateAuthMiddlewareRequirePassword::class, 'signed' => IlluminateRoutingMiddlewareValidateSignature::class, 'throttle' => IlluminateRoutingMiddlewareThrottleRequests::class, 'verified' => IlluminateAuthMiddlewareEnsureEmailIsVerified::class, 'adminLogin'=>AppHttpMiddlewareAdminLogin::class, 'doctorLogin'=>AppHttpMiddlewareDoctorLogin::class, 'patientLogin'=>AppHttpMiddlewarePatientLogin::class, 'userLogin'=>AppHttpMiddlewareUserLogin::class, 'appDoctorLogin'=>AppHttpMiddlewareAppDoctorLogin::class, 'weChatAppLogin'=>AppHttpMiddlewareWeChatAppLogin::class, 'hospitalLogin'=>AppHttpMiddlewareHospitalLogin::class, 'diyapp'=>AppHttpMiddlewareAccessControllerAllowOrigin::class, ];
这样就可以去使用自己的路由了