我需要接收 http 状态错误,m.request
因此我extract
按照文档使用。但由于某种原因,它弄乱了我的数据返回。
根据文档,如果我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)})
现在,我在成功和错误回调中都获得了错误的状态。我需要获取错误状态和成功数据。我该怎么做呢?我究竟做错了什么?这是我的代码:
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);
});
}
};
这里的错误和数据都给了我状态码。我需要获取成功的传入数据来设置我的身份验证。
谢谢。