您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關Laravel API允許跨域訪問的示例分析的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
服務器A請求服務器B的接口,那么一般會出現跨域問題。全解跨域請求處理辦法
XMLHttpRequest cannot load http://api.console.vms3.com/api/user. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' istherefore not allowed access.
意思就是服務器響應不允許跨域訪問.
那我們就需要讓服務器支持跨域訪問, 也就是在響應頭部中添加
"'Access-Control-Allow-Origin: *' "
創建 `app/Http/Middleware/AccessControlAllowOrigin.php` middleware 把 'Access-Control-Allow-Origin: *' 寫入頭部. app/Http/Middleware/AccessControlAllowOrigin.php <?php namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Auth; class AccessControlAllowOrigin { /** * * Handle an incoming request. * * @param \Illuminate\Http\Request $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); } }
注冊這個 middleware
到 kernel
中.
分別在 protected $middleware
數組中和 protected $routeMiddleware
數組中
添加我們剛才創建的那個文件class
名, 使用 cors
這個別名.
然后在設置它保護 api , 就是$middlewareGroups['api']
的數組中添加它的別名, 本文中是 'cors'
app/Http/Kernel.php
<?php namespace App\Http; use Illuminate\Foundation\Http\Kernel as HttpKernel; class Kernel extends HttpKernel { /** * The application's global HTTP middleware stack. * * These middleware are run during every request to your application. * * @var array */ protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, \App\Http\Middleware\TrimStrings::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, \App\Http\Middleware\AccessControlAllowOrigin::class, ]; /** * The application's route middleware groups. * * @var array */ protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, // \Illuminate\Session\Middleware\AuthenticateSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, ], 'api' => [ 'throttle:60,1', 'bindings', 'cors' ], ]; /** * The application's route middleware. * * These middleware may be assigned to groups or used inpidually. * * @var array */ protected $routeMiddleware = [ 'auth' => \Illuminate\Auth\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'cors' => \App\Http\Middleware\AccessControlAllowOrigin::class, ]; }
Route::middleware('cors')->group(function () { // });
感謝各位的閱讀!關于“Laravel API允許跨域訪問的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。