Javascript MithrilJS:如何使用m.request()获取错误的XHR.status和成功的数据

Javascript MithrilJS:如何使用m.request()获取错误的XHR.status和成功的数据,javascript,mithril.js,Javascript,Mithril.js,我需要接收带有m.request的http状态错误,因此我根据文档使用extract。但由于某种原因,它把我的数据返回搞砸了 根据docs,如果我使用extract获取状态,那么extractreturn作为参数传递给错误回调,数据传递给成功回调。下面是文档中的片段 var nonJsonErrors = function(xhr) { return xhr.status > 200 ? JSON.stringify(xhr.responseText) : xhr.responseTe

我需要接收带有
m.request
的http状态错误,因此我根据文档使用
extract
。但由于某种原因,它把我的数据返回搞砸了

根据docs,如果我使用
extract
获取状态,那么
extract
return作为参数传递给错误回调,数据传递给成功回调。下面是文档中的片段

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)})
现在,我在success和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);
    });
  }
};
这里的错误和数据都给我状态代码。我需要获取成功时的传入数据以设置身份验证


谢谢。

好的。我想出来了。我愚蠢到错过了我自己发布的文档片段中的条件。我认为
extract
会在出现错误时返回,但在这两种情况下都会返回,您需要在extract定义中决定是返回状态代码还是响应主体。明白了