2016-04-01 95 views
2

我正在嘗試使用Aurelia HttpClient在不同域(和身份驗證)上調用TagniFi API。相關的代碼是:Aurelia帶身份驗證的HttpClient

return this._httpClient.createRequest(uri) 
    .asGet() 
    .withCredentials(true) 
    .withHeader('Access-Control-Allow-Origin', 'localhost:9000') 
    .withHeader('Authorization', 'Basic <my-key>') 
    .send(); 

我收到來自API狀態200,但我得到了以下錯誤:

Response to preflight request doesn't pass access control check: A >wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header >when the credentials flag is true. Origin ' http://localhost:9000 ' is >therefore not allowed access. The credentials mode of an XMLHttpRequest >is controlled by the withCredentials attribute.

我不知道這是我的方式錯誤撥打電話或Aurelia中的錯誤。任何幫助表示讚賞。謝謝。

回答

2

訪問控制允許來源是由服務器在響應中發送的標頭,在您的情況下,您在客戶端的請求中使用它。

這不是Aurelia中的錯誤,而是錯誤的CORS。

有關CORS的更多信息,請參閱this excellent answer

+0

謝謝瓦倫丁,感謝您的回覆。所以,如果我理解正確,問題是我所調用的目標服務器未配置爲返回Access-Control-Allow-Origin標頭。換句話說,這不是我的代碼中的錯誤,而是服務器上的配置錯誤?如果這是我的代碼中的錯誤,請告訴我,如果您有建議的修復程序。謝謝。 – sevenshadow