反应运行时。发送请求:
fetch('https://my-site.ru/bla/bla-bla/get-token', {
method: 'POST',
body: JSON.stringify({
username: 'username',
password: 'password'
})
})
.then(res => {
if (res.status >= 200 && res.status < 300 ) {
return res;
} else {
let error = new Error(res.statusText);
error.response = res;
throw error
}
})
.then((res) => {
if (res.headers['content-type'] !== "application/json; charset=UTF-8") {
let error = new Error('Некорректный ответ от сервера');
error.response = res;
throw error
} else func(res.data)
}).catch(e => {
console.log(e) //выведет 'Error [object promice]'
console.log(e.message) // Выведет '[object promice]'
})
如果名称或密码不正确,服务器会返回 403 并在 DevTooll 选项卡上显示一条消息,说明究竟是什么错误。但我似乎无法抓住这个消息。我遇到了 403 错误,但我不明白如何从服务器获取消息。
这就是 Devtools 中“响应”选项卡上的样子
{"success":false,"statusCode":403,"code":"invalid_username","message":"\u041e\u0448\u0438\u0431\u043a\u0430: \u0418\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f asd \u043d\u0435 \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u043d\u0430 \u0441\u0430\u0439\u0442\u0435. \u0415\u0441\u043b\u0438 \u0432\u044b \u0437\u0430\u0431\u044b\u043b\u0438 \u0438\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0432\u043c\u0435\u0441\u0442\u043e \u043d\u0435\u0433\u043e \u0430\u0434\u0440\u0435\u0441 email.","data":[]}
如何获取此对象,屏幕截图和显示消息中的内容是什么?



发现了问题所在。这都是关于 .json() .then 和线程的。立即,工作代码:
我还没有足够的知识来详细解释,但是有一些与进程的线程有关的东西。只是信息在被解析之前就进入了catch,所以似乎有必要在.json()之后使用then
这是一个非常相关的讨论:https ://stackoverflow.com/questions/37555031/why-does-json-return-a-promise