【问题标题】:Laravel API CORSLaravel API CORS
【发布时间】:2018-12-03 02:46:58
【问题描述】:

我使用 Laravel 5.6 创建了 API,并创建了 CORS 中间件和 this is the content of middleware,但是当从另一个 IP 地址访问此 api 时,它会返回数据..

Route::group([

'middleware' => 'cors',

], function ($router) {


Route::get('/get_users', function(){
     return App\User::all();
});

});

我在kernal.php中注册了cors中间件

【问题讨论】:

  • 嗨,欢迎来到堆栈溢出。请参阅How to Ask 链接以获取有关如何提出问题并相应更新您的问题的更多详细信息。

标签: php laravel api cors


【解决方案1】:

只需将其添加到您的 api 路由文件的开头即可:

header('Access-Control-Allow-Origin : *');
header('Access-Control-Allow-Headers : Content-Type,X-Auth-Token,Authorization,Origin');
header('Access-Control-Allow-Methods :GET, POST, PUT, DELETE, OPTIONS');

这对我有用。

【讨论】:

    【解决方案2】:

    您可以使用barryvdh/laravel-cors 包而不是编写自己的中间件。

    【讨论】:

      猜你喜欢
      • 2020-01-04
      • 2016-01-09
      • 2019-07-03
      • 2020-10-20
      • 2021-01-01
      • 2021-11-17
      • 2015-09-30
      • 2015-02-19
      相关资源
      最近更新 更多