我正在使用 axios 向端點發出包含數據的帖子請求:這有效:import axios from 'axios';axios.post('https://example.com/v1/login', { name: 'myuser', password: 'mypassword',});但事實并非如此import axios from 'axios';export const apiBase = axios.create({ baseURL: "https://example.com/v1/", withCredentials: true, headers: { 'Content-Type': 'application/json;charset=UTF-8', },});apiBase.post('login', { name: 'myuser', password: 'mypassword',});哪些日志:Access to XMLHttpRequest at 'https://example.com/v1/login' from origin 'http://example.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.實際上,沒有向此請求添加標頭。'Content-Type'有誰知道這里出了什么問題?編輯:修改以下評論import axios from 'axios';export const apiBase = axios.create({ baseURL: "https://example.com/v1/", withCredentials: true, headers: { 'Content-Type': 'application/json;charset=UTF-8', },});apiBase.defaults.headers['Content-Type'] = 'pplication/json;charset=UTF-8';apiBase.defaults.headers['Access-Control-Allow-Origin'] = 'http://example.com';apiBase.defaults.headers['Host'] = 'example.com';apiBase.defaults.headers['Referer'] = 'example.com';apiBase.defaults.headers['Accept-Encoding'] = 'gzip, deflate, br';apiBase.post('login', { name: 'myuser', password: 'mypassword',});它仍然返回Access to XMLHttpRequest at 'https://example.com/v1/login' from origin 'http://example.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
Axios.create 和 CORS
炎炎設計
2022-08-04 10:19:44