2

我需要接收 http 状态错误,m.request因此我extract按照文档使用。但由于某种原因,它弄乱了我的数据返回。

根据文档,如果我extract用来获取状态,则extractreturn 作为参数传递给错误回调,数据传递给成功回调。这是来自文档的片段。

var nonJsonErrors = function(xhr) {
  return xhr.status > 200 ? JSON.stringify(xhr.responseText) : xhr.responseText
}

m.request({method: "GET", url: "/foo/bar.x", extract: nonJsonErrors})
  .then(function(data) {}, function(error) {console.log(error)})

现在,我在成功和错误回调中都获得了错误的状态。我需要获取错误状态和成功数据。我该怎么做呢?我究竟做错了什么?这是我的代码:

var Application = {
  run() {
    m.request({
      method: "GET",
      url: "http://localhost:3000/api/session/ping",
      extract(xhr) {return xhr.status;}
    }).then((data) => {
      console.log("Session is Up");
      console.log(data);
      var init = {
        uname: data.uname
      };
      router(init);
    }, (error) => {
      console.log(`Cought: ${error}`);
      m.mount(document.body, Login);
    });
  }
};

这里的错误和数据都给了我状态码。我需要获取成功的传入数据来设置我的身份验证。

谢谢。

4

1 回答 1

2

好的。我想到了。我很愚蠢,以至于错过了我自己发布的文档片段中的条件。我认为extract在发生错误的情况下返回,但它在两种情况下都会返回,您需要在提取定义中自行决定是返回状态代码还是响应正文。知道了。

于 2015-07-15T07:35:07.170 回答