炎炎設(shè)計(jì)
2022-08-04 10:19:44
我正在使用 axios 向端點(diǎn)發(fā)出包含數(shù)據(jù)的帖子請(qǐng)求:這有效:import axios from 'axios';axios.post('https://example.com/v1/login', { name: 'myuser', password: 'mypassword',});但事實(shí)并非如此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.實(shí)際上,沒(méi)有向此請(qǐng)求添加標(biāo)頭。'Content-Type'有誰(shuí)知道這里出了什么問(wèn)題?編輯:修改以下評(píng)論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.
2 回答

慕碼人2483693
TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個(gè)贊
設(shè)置憑據(jù)或?qū)?nèi)容類型設(shè)置為 JSON 將觸發(fā)預(yù)檢請(qǐng)求。
對(duì)預(yù)檢的響應(yīng)的要求比對(duì)非預(yù)檢請(qǐng)求的要求更嚴(yán)格。
其中之一是,正如錯(cuò)誤消息所說(shuō),您不能使用通配符。您必須顯式指定原點(diǎn)。你沒(méi)有那樣做。
實(shí)際上,沒(méi)有向此請(qǐng)求添加標(biāo)頭“內(nèi)容類型”。
自然。印前檢查未獲得設(shè)置它的權(quán)限,因此從未發(fā)出請(qǐng)求。

喵喵時(shí)光機(jī)
TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
無(wú)論出于何種原因,Axios 都不允許在將 withCredentials 設(shè)置為 true 時(shí)以 JSON 對(duì)象的形式在 POST 請(qǐng)求中發(fā)送數(shù)據(jù)。在傳遞數(shù)據(jù)之前,您需要將數(shù)據(jù)字符串化。
添加回答
舉報(bào)
0/150
提交
取消