尝试理解使用我的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的新闻? –