php实现跨域

Php跨域

  1. 在D:\svn\bugulife\bugu_front\app\Http\Kernel.php里,增加

    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\CORS::class, //跨域访问中间件

    ];

2. 增加文件D:\svn\bugulife\bugu_front\app\Http\Middleware\CORS.php,内容:

namespace App\Http\Middleware;

use Closure;

 

class CORS

{

    public function handle($request, Closure $next)

    {

        return $next($request)->header('Access-Control-Allow-Origin', '*')

            ->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE');

    }

}

你可能感兴趣的:(PHP)