【发布时间】:2019-02-26 17:25:47
【问题描述】:
我正在使用 ReactJS 发出 put 请求,但是,当我输入错误的电子邮件/密码组合时,我会在 Chrome 上登录,即使我试图捕获所有错误并将它们显示在 errorDiv :
async connect(event) {
try {
const userObject = {
username: this.state.userName,
password: this.state.password
};
if (!userObject.username || !userObject.password) {
throw Error('The username/password is empty.');
}
let response = await fetch(('someurl.com'), {
method: "PUT",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(userObject)
});
let resJSON = await response.json();
if (!response.ok) {
throw Error(resJSON.message);
}
console.info(resJSON.message);
console.info(resJSON.message.auth_token);
window.location = "/ledger/home";
} catch (e) {
document.getElementById("errorDiv").style.display = 'block';
document.getElementById("errorDiv").innerHTML = e;
}
}
【问题讨论】:
-
代码没问题。问题无法复制,stackblitz.com/edit/react-pb51rz
标签: javascript reactjs put http-status-code-403 fetch-api