【发布时间】:2018-09-21 05:15:09
【问题描述】:
我在 React 中使用下面的 JS 代码来获取用户名和密码。
当我将用户名的输入类型用作 text 时,我得到“TypeError: Failed to fetch”,但如果我将类型切换为 email,那么 fetch 工作正常。
这是为什么呢?我不希望输入类型为电子邮件,因为它会在提交表单时提示我进行其他检查,例如 @ 符号。
我的 JS:
login() {
const userObject = {
username: document.getElementById('inputUsername').value || null,
password: document.getElementById('inputPassword').value || null
};
this.connect('someurl.com', userObject);
}
async connect(url, userObject) {
try {
let response = await fetch((url), {
method: "PUT",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(userObject)
});
} catch (e) {
console.error(e);
}
}
我的 HTML 输入:
<input type="text" id="inputUsername" />
【问题讨论】:
-
你从哪里得到这个错误?另外,你能在 sn-p 中创建工作示例吗?
-
我在浏览器上记录了它,当我使用它时它可以工作:
<input type="email" id="inputUsername" /> -
请创建相同的演示。
标签: javascript html reactjs