2015-07-10 52 views
1

我試圖讓一個跨源前端在另一個域託管的SailsJS後端正常運行。具體來說,用戶認證和憑證是破碎的。如何獲取使用Sails發送憑據的跨站點XMLHttpRequest調用?

一切行爲正常與POST, GET,等類型的數據庫行爲。例如,新用戶的註冊已經起作用。

但是,用戶登錄後,身份驗證步驟失敗,我收到401 Unauthorized

這裏是控制檯告訴我:

[Object, "post", "login", "https://small-change-api.herokuapp.com/user/login"] 
User logged in: Object {firstName: "John", lastName: "Does", email: "[email protected]", activated: true, superUser: false…} 

- 不錯:)

[Array[0], "get", "authenticated", "https://small-change-api.herokuapp.com/user/authenticated"] 
GET https://small-change-api.herokuapp.com/user/authenticated 401 (Unauthorized) 

- 不好

現在,這一切的作品本地100%,但託管在Heroku上,出現問題:/

下面是一些相關的服務器端配置: 這是文件config/cors.js

module.exports.cors = { 

    allRoutes: true, 
    origin: 'https://thefrontendorigin.com', 
    credentials: true, 
    methods: 'GET, POST, PUT, DELETE, OPTIONS, HEAD, PATCH', 
    headers: 'content-type,Access-Control-Allow-Origin' 

}; 

在前端withCredentials設置爲true:

RestangularProvider.setDefaultHttpFields({ withCredentials: true }); 

還有什麼我需要得到正確的?它似乎應該工作...:/

讓我知道如果我需要包含任何更多的信息!

謝謝。

P.S. GH回購現在都是公開的,所以讓我分享一下。

Front-end Github

Back-end Github

回答

1

我需要做什麼,是進入CORS設置(跨域請求),並加入這一行:

headers: 'content-type,Access-Control-Allow-Credentials'

這是關鍵我失蹤了,希望能幫助別人。

相關問題