LoginSignup
7
6

More than 5 years have passed since last update.

SPAモードのNuxt開発サーバーからLaravelのAPIへアクセスする

Posted at

やりたいこと

同一サーバー上でLaravelのAPI(port:80)と、SPAモードのNuxt開発サーバー(port:3000)が共存している状態でNuxt(ブラウザ)からAPIにAjaxでアクセスしたい。

環境

Laravel 5.7.9
Nuxt.js 2.2.0

問題点

デフォルトだとCORSによりNuxt(ブラウザ)からLaravelにアクセスが出来ない。

対応方法

LaravelのCORS対応ライブラリを導入する。

barryvdh/laravel-cors

導入方法は基本的にマニュアル通りだが、許可するアクセス元を念の為に下記のように制限しておく。

config/cors.php
    //'allowedOrigins' => ['*'], ※こちらがデフォルト
    'allowedOrigins' => ['http://localhost:3000'],

以上でアクセスが出来るようになった。

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6