我在Heroku上有一個應用程序http://random-name.herokuapp.com,它發送帶有Mandrill的電子郵件。但是,無論我是否在本地運行在localhost:5000
或遠程應用程序在Heroku上的,試圖發送電子郵件時,我拋出了以下錯誤:Mandrill XMLHttpRequest錯誤 - 無效的訪問控制 - 允許源標頭
的XMLHttpRequest無法加載 https://mandrillapp.com/api/1.0/messages/send.json。對 預檢請求的響應未通過訪問控制檢查:當 憑證標誌爲true時,通配符'*' 無法用於'Access-Control-Allow-Origin'標頭中。 Origin'http://random-name.herokuapp.com'是 因此不允許訪問。
有堆棧溢出關於此錯誤的大量的文檔資料(見CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true),因此,我把我的快遞頭部以下:
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:5000 http://random-name.herokuapp.com");
res.header('Access-Control-Allow-Credentials', true);
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
然而,這似乎並沒有解決問題。我在Mandrill的文檔中找不到任何有用的東西。我猜測我錯誤地指定了標題,或者甚至沒有被使用。有任何想法嗎?
所以它看起來像他們響應頭通配符: '訪問控制允許來源:* 內容編碼:gzip 的Content-Type:text/html的; charset = utf-8' –
你是什麼意思「返回任何客戶端發送」? –
@sir_thursday - 您嘗試使用通配符,並且錯誤消息清楚地告訴您,因爲您設置了'withCredentials',因此不允許使用通配符。 – Quentin