問題描述
我已經閱讀了很多關于 preflight 和 CORS 的答案,所以請不要發布鏈接引用我應該閱讀的內容.許多答案都是從服務器的角度來看的,但在這種情況下,我是客戶.我是否設置了原始標頭?我的假設是這是一個簡單的請求,對嗎?
I've read many answers of preflight and CORS so please do not post links referencing what I should read. Many of the answers are from a server-perspective, but I am the client in this case. Do I set the origin header? My assumption is that this is a simple request, am I right?
req.open("POST", url, true);
req.setRequestHeader( 'Content-Type', 'application/blahblah' );
req.setRequestHeader( 'Accept', 'application/blahblah' );
req.setRequestHeader("Authorization", "Basic " + btoa(user + ":" + pass));
req.send();
但它仍然無法正常工作,我的錯誤:
Yet its still not working, my error:
對預檢請求的響應未通過訪問控制檢查:請求的資源上不存在Access-Control-Allow-Origin"標頭.因此,Origin 'null' 不允許訪問.響應的 HTTP 狀態代碼為 500.
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 500.
推薦答案
解決方案:
var req = new XMLHttpRequest();
req.setRequestHeader('Authorization', 'Basic [base64 encoded password here]' );
這篇關于如何將基本身份驗證標頭分配給 XMLHTTPREQUEST?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!