嘗試理解使用我的api和ember-cli-mirage之間的響應不一致性。從XHR對象中缺少ember-cli-mirage響應標頭
我有一個處理程序等待POST請求的響應來驗證用戶。處理器的預期參數是response
,status
和xhr
:
(例如.then(function(response, status, xhr) {...}
)。
使用我的API我收到我期望的結果 - 響應是數據,status是statusText,xhr是xhr對象。然而,使用ember-cli-mirage一切都在響應(類型)之下,而status和xhr都是未定義的。我的代碼
片斷低於:
海市蜃樓/ config.js
this.post(URI.AUTH_SIGN_IN, function(db, request) {
const responseHeaders = {
'access-token': 'abcxyz123',
'client': 'foobarbaz',
'token-type': 'Bearer',
'expiry': '1497364419',
'uid': '[email protected]'
};
const user = {
data: { id: 1, type: 'user', attributes: { uid: '[email protected]', email: '[email protected]', name: 'John Doe', provider: 'email' } }
};
return new Mirage.Response(200, responseHeaders, user);
});
鑑別器/ devise.js
authenticate(identification, password) {
...
this.makeRequest(credentials).then(function(response, status, xhr) {
// persists the five headers needed to send to devise-token-auth
// with mirage; response = Response {type: "default", status: 200, ok: true, statusText: "OK", headers: Headers…}, status = undefined, xhr = undefined
// with actual api; response = Object {data: Object}, status = "success", xhr = Object {readyState: 4, getResponseHeader: function, getAllResponseHeaders: function…}
// As a result below will fail :(
// TypeError: Cannot read property 'getResponseHeader' of undefined
var result = {
'access-token': xhr.getResponseHeader('access-token'),
expiry: xhr.getResponseHeader('expiry'),
tokenType: xhr.getResponseHeader('token-type'),
uid: xhr.getResponseHeader('uid'),
client: xhr.getResponseHeader('client')
};
});
}
我相信我做所有正確的,但我知道是錯誤的:)。任何幫助深表感謝。
這個@TomDoe的新聞? –