LoginSignup
0
1

More than 3 years have passed since last update.

備忘録 laravel react axios CORSエラーを出ないようにする。

Posted at

laravelにreactを入れてaxiosを書いて、外部apiにアクセスするとCORSエラーになる場合があります。

原因は、resources/js/bootstrap.js

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

です。

以下のようにするとエラーが出ないようになります。

// resources/js/components内のファイルなどで記載
const extAxios = axios.create();
delete extAxios.defaults.headers.common["X-Requested-With"];

function Example() {

  useEffect(() => {
      extAxios.get("https://aws.random.cat/meow").then((res) => {
          console.log(res.data.file);
      });
  }, []);

// 以下省略

こちら参考にさせて頂きました。

0
1
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
0
1