1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

axiosのCORS対処

Posted at

問題

axiosを使って、firebase dynamiclinksのREST apiをcallした時、なぜかCORS関連のエラーが出た。
XMLHttpRequestを使ったら出ず。

対処法

両者を比較したら、withCredentials = falseが XMLHttpRequest でデフォルトになっている。
それが原因だとわかった。
ただ、通常の axios.post(..., { headers })で書いても、withCredentialsがうまく設定できず、
以下のような書き方にした

const axiosInstance = axios.create({
    withCredentials: false,
    headers: {
      "Content-Type": "application/json",
    },
  });
  return axiosInstance.post(`${FIREBASE_DYNAMIC_LINK_API}?key=${API_KEY}`, {
    dynamicLinkInfo: {
      domainUriPrefix: DYNAMIC_LINK_PREFIX,
      link: `${FUNCTION_BASE_URL}share?imgUrl=${link}`,
    },
    suffix: {
      option: "SHORT",
    },
  });
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?