跨域請求中的withCredentials、Access-Control-Allow-Origin

Access to XMLHttpRequest at 'https://34cd046f.ngrok.io/node/vcode/123456543' from origin 'null' has been blocked by CORS policy: 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.
意思是:使用xhr對象訪問」https:xxxx「時,請求頭中的origin: null,因此被cors策略阻止了。
當請求的憑據模式爲include時,回饋頭的Access-Control-Allow-Origin必定不能爲'*'
在請求被初始化時使用xhr對象的withCredentials屬性控制憑證模式。前端

withCredentials

withCredentials是xhr對象的一個屬性。它的值是boolean。若爲true,則執行跨域請求時在請求頭中挾帶憑據信息。不然不挾帶。憑據信息包括cookie、http認證、客戶端ssl……。經常使用cookie爲憑據。
axios是基於(但不限於)xhr+promise作出來的。axios也有withCredentials屬性。node

resolution

在回饋頭中設置Access-Control-Allow-Origin爲請求者的域名。ios

extension

上面的報錯問題中提出的」憑據模式爲include「。感受還會有別的模式。都有什麼模式,我也不知道。
爲何設置withCredentials: true,就能夠讓憑據模式變爲include呢?
回饋頭中的Access-Control-Allow-Credentials(其值爲boolean)表示是否把回饋暴露給前端。
跨域問題是前端、後端溝通交流時出現的問題。不是中有前端或後端能解決的,須要前、後端協同解決。axios


2020/05/16 by stone後端

相關文章
相關標籤/搜索