亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

訪問 XMLHttpRequest:https://identitytoolkit.

訪問 XMLHttpRequest:https://identitytoolkit.

慕碼人2483693 2023-10-14 18:54:59
從源“http://localhost:8080”訪問“https://identitytoolkit.googleapis.com/v1/accounts:/signUp?key=AIzaSyDvZN5PKdB_b9jX-5rA-t9e3KSxJ-qtkdU”處的 XMLHttpRequest 已被 CORS 策略阻止:對預檢請求的響應未通過訪問控制檢查:無“Access-Control-Allow-Origin”我嘗試在 vue.js 中使用 Axios 在 firebase 中創建新用戶,但出現上述錯誤。這是我的代碼示例。onSubmit () {    const formData = {      email: this.email,      age: this.age,      password: this.password,      confirmPassword: this.confirmPassword,      country: this.country,      hobbies: this.hobbyInputs.map(hobby => hobby.value),      terms: this.terms    }    console.log(formData)    axios.post('signUp?key=AIzaSyDvZN5PKdB_b9jX-5rA-t9e3KSxJ-qtkdU', {      email: formData.email,      password: formData.password,      returnSecureToken: true,    })    .then((res) => {        console.log(res)    })    .catch((error) => {      console.log(error)    })  }這是我的默認 axios 實例代碼。import axios from 'axios'const instance = axios.create({    baseURL: 'https://identitytoolkit.googleapis.com/v1/accounts:'});instance.defaults.headers.common['Access-Control-Allow-Headers'] = 'X-Requested-With, content-type';instance.defaults.headers.common['Access-Control-Allow-Origin'] = 'X-Requested-With, content-type';export default instance;
查看完整描述

1 回答

?
達令說

TA貢獻1821條經驗 獲得超6個贊

CORS 是一個非常煩人的錯誤,實際上它是一場噩夢。

解決此問題的一種方法是使用代理,例如https://cors-anywhere.herokuapp.com。

獲取示例:

 var proxyUrl = 'https://cors-anywhere.herokuapp.com/',

    targetUrl = 'https://request-url.com'

fetch(proxyUrl + targetUrl)

  .then(blob => blob.json())

  .then(data => {

     var stringed = JSON.stringify(data);

     const res = JSON.parse(stringed);


     // Use res to access JSON

  })

  .catch(e => {

    console.log(e);

    return e;

  });

當然,這個例子只是為了獲取,但我相信你可以修改它以與 axios 一起使用。


如果您使用 NodeJS,請添加行const fetch = require("node-fetch");.


查看完整回答
反對 回復 2023-10-14
  • 1 回答
  • 0 關注
  • 260 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號