慕碼人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 策略阻止:對預(yù)檢請求的響應(yīng)未通過訪問控制檢查:無“Access-Control-Allow-Origin”我嘗試在 vue.js 中使用 Axios 在 firebase 中創(chuàng)建新用戶,但出現(xiàn)上述錯誤。這是我的代碼示例。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條經(jīng)驗 獲得超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,請?zhí)砑有衏onst fetch = require("node-fetch");.
添加回答
舉報
0/150
提交
取消