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

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

Axios.create 和 CORS

Axios.create 和 CORS

炎炎設計 2022-08-04 10:19:44
我正在使用 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.
查看完整描述

2 回答

?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

設置憑據或將內容類型設置為 JSON 將觸發預檢請求。

對預檢的響應的要求比對非預檢請求的要求更嚴格。

其中之一是,正如錯誤消息所說,您不能使用通配符。您必須顯式指定原點。你沒有那樣做。

實際上,沒有向此請求添加標頭“內容類型”。

自然。印前檢查未獲得設置它的權限,因此從未發出請求。


查看完整回答
反對 回復 2022-08-04
?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

無論出于何種原因,Axios 都不允許在將 withCredentials 設置為 true 時以 JSON 對象的形式在 POST 請求中發送數據。在傳遞數據之前,您需要將數據字符串化。


查看完整回答
反對 回復 2022-08-04
  • 2 回答
  • 0 關注
  • 221 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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