我的html页面中有一个表单,我将它添加到JS中,现在我想获取一个URL来添加我的数据。这是我的代码:
表单的结果:
let password = document.getElementById("Password")
let Mail = document.getElementById("Mail")
let username = document.getElementById("Username")
将结果添加到元素
let data = {
email: Mail,
password: password,
nickName: username
}
获取链接
fetch('The Link', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Credentials' : 'include',
},
body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => {
if (response === 200){
return data.json();
}
else{
throw 'error with server status';
}
})
.catch((error) => {
fout.appendChild(
document.createTextNode(error));
});
您的状态检查应该在前面的then()
中响应对象存在的地方
fetch('The Link', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Credentials': 'include',
},
body: JSON.stringify(data),
})
.then(response => {
if (!response.ok) {
throw new Error('error with server status');
}
return response.json()
})
.then(data => {
// consume the data
})
.catch((error) => {
fout.appendChild(
document.createTextNode(error.message));
});