Javascript 使用JS将表单数据提取到URL
我在我的html页面中有一个表单,我把它添加到JS中,现在我想获取一个URL来添加我的数据。这是我的代码: 表格中的结果:Javascript 使用JS将表单数据提取到URL,javascript,html,json,fetch,Javascript,Html,Json,Fetch,我在我的html页面中有一个表单,我把它添加到JS中,现在我想获取一个URL来添加我的数据。这是我的代码: 表格中的结果: let password = document.getElementById("Password") let Mail = document.getElementById("Mail") let username = document.getElementById("Username") 将结果添加到元素中 le
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));
});
你的问题是什么?@SiCK我怎样才能用JS从我的html页面获取数据到URL?那么我必须在.then(data=>{….}中添加什么?这就是你处理数组或对象的地方。如果数组类似于
data.forEach(value=>/*使用value*/)
Okey抱歉,我是一个真正的初学者,所以我不明白…这是我需要发送的数据=>`let data={email:Mail,passwordV,昵称:username}`No。then()中的数据是发送后从服务器返回的数据,但数据不在需要的地方