简体   繁体   English

XHR对象中缺少ember-cli-mirage响应头

[英]ember-cli-mirage response headers are missing from XHR object

Trying to understand the inconsistency in the response between using my api vs ember-cli-mirage. 试图理解使用我的api与ember-cli-mirage之间的反应不一致。

I have a handler waiting for a response for a POST request to authenticate a user. 我有一个处理程序等待POST请求的响应以验证用户。 The expected parameters for the handler are response , status and xhr : 处理程序的预期参数是responsestatusxhr

(eg .then(function(response, status, xhr) {...} ). (例如.then(function(response, status, xhr) {...} )。

Using my API I receive what I'd expect- response is the data, status is the statusText and xhr is the xhr object. 使用我的API我得到了我期望的 - 响应是数据,状态是statusText,xhr是xhr对象。 However using ember-cli-mirage everything comes under response (kind of), and status and xhr are both undefined. 然而,使用ember-cli-mirage,一切都在响应(种类),状态和xhr都是未定义的。

Snippets of my code are below: 我的代码片段如下:

mirage/config.js 海市蜃楼/ config.js

this.post(URI.AUTH_SIGN_IN, function(db, request) {
  const responseHeaders = {
    'access-token': 'abcxyz123',
    'client': 'foobarbaz',
    'token-type': 'Bearer',
    'expiry': '1497364419',
    'uid': 'user@example.com'
  };

  const user = {
    data: { id: 1, type: 'user', attributes: { uid: 'user@example.com', email: 'user@example.com', name: 'John Doe', provider: 'email' } }
  };

  return new Mirage.Response( 200, responseHeaders, user );
});

authenticators/devise.js 鉴定人/ 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' )
      };
   });
}

I believe I'm doing it all correctly, but I've been known to be wrong :). 我相信我做得很好,但我知道错了:)。 Any help is much appreciated. 任何帮助深表感谢。

Hm, I'm not sure why makeRequest is returning undefined for the second and third params. 嗯,我不知道为什么makeRequest返回undefined为第二和第三个参数。

I made a simple Twiddle and the args seem correct for getJSON : 我做了一个简单的Twiddle,args看起来对于getJSON正确的:

https://ember-twiddle.com/70229e352f37b4e437ced8509a4415d9?openFiles=routes.application.js%2C https://ember-twiddle.com/70229e352f37b4e437ced8509a4415d9?openFiles=routes.application.js%2C

model() {
  return Ember.$.getJSON('/foo').then((data, response, object) => {
    return object.getAllResponseHeaders();
  });
}

There may be something slightly different with how Pretender handles the mocked response or iwth how makeRequest works, so I'd suggest to start by looking there. 可能有一些与伪装者如何处理嘲笑响应或iwth如何略有不同makeRequest工作,所以我建议看那里开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM