我需要使用m.request
接收http状态错误,因此我根据文档使用extract
。但由于某种原因,它会弄乱我的数据。
根据文档,如果我使用extract
获取状态,则extract
return将作为参数传递给错误回调,并将数据传递给成功回调。以下是docs的片段。
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);
});
}
};
这里的错误和数据都给我了状态代码。我需要获取成功的传入数据来设置我的身份验证。
感谢。
答案 0 :(得分:2)
行。我想到了。我愚蠢到错过了我自己发布的文档片段中的条件。我认为extract
会在出现错误时返回,但它会在两种情况下都返回,您需要在提取定义中自行决定是返回状态代码还是响应正文。明白了。