Javascript 需要帮助重定向获取吗
我对后端编程有点陌生,所以我有点受不了 我正试图从我的登录网页重定向到第二个页面,标题为“gerente”。我一直在使用fetch,因为我也在使用mongoDB来保存登录数据,但我似乎没有任何运气在单击登录按钮后重定向,但数据正在保存 丢失的代码是什么 这是我在index.html中的表单:Javascript 需要帮助重定向获取吗,javascript,html,node.js,fetch,Javascript,Html,Node.js,Fetch,我对后端编程有点陌生,所以我有点受不了 我正试图从我的登录网页重定向到第二个页面,标题为“gerente”。我一直在使用fetch,因为我也在使用mongoDB来保存登录数据,但我似乎没有任何运气在单击登录按钮后重定向,但数据正在保存 丢失的代码是什么 这是我在index.html中的表单: <form name="form" > <div class="form-control"&
<form name="form" >
<div class="form-control">
<label for="username">Usuario</label>
<input type="text" name="username" id="username" placeholder="Ingrese su usuario..." required />
</div>
<div class="form-control">
<label for="room">Equipo</label>
<select name="room" id="room">
<option value="rojo">Equipo rojo</option>
<option value="amarillo">Equipo Amarillo</option>
<option value="blanco">Equipo Blanco</option>
<option value="azul">Equipo Azul</option>
<option value="negro">Equipo Negro</option>
</select>
</div>
<button id="login-gerente" type="button" class="btn" >Gerente</button>
<button id="login-mecanico" type="button" class="btn" >Mecanico</button>
</form>
</main>
尝试在
功能中设置重定向,然后设置功能:
window.onload = () => {
document.getElementById("login-gerente").onclick = () => {
console.log("inicio");
//recoger los datos del formulario
let user = {};
user.username = document.getElementById("username").value;
user.room = document.getElementById("room").value;
console.log(user);
//crear el Json de la petición
//peticion post: utilizar fetch
fetch("/jugadores", {
method: 'post',
headers: {
"Content-type": "application/json"
},
body: JSON.stringify(user)
})
.then(json)
.then(function (data) {
console.log('Request succeeded with JSON response', data);
//redirección
window.location.replace("/Gerente.html")
})
.catch(function (error) {
console.log('Request failed', error);
//redirección
window.location.replace("/Gerente.html")
});
//validación de usuario
}
};
这似乎奏效了,谢谢。此外,我注意到在StrugType中我有一个打印错误,我调用JSON而不是用户,请考虑批准这个答案,如果它有帮助的话:
window.onload = () => {
document.getElementById("login-gerente").onclick = () => {
console.log("inicio");
//recoger los datos del formulario
let user = {};
user.username = document.getElementById("username").value;
user.room = document.getElementById("room").value;
console.log(user);
//crear el Json de la petición
//peticion post: utilizar fetch
fetch("/jugadores", {
method: 'post',
headers: {
"Content-type": "application/json"
},
body: JSON.stringify(user)
})
.then(json)
.then(function (data) {
console.log('Request succeeded with JSON response', data);
//redirección
window.location.replace("/Gerente.html")
})
.catch(function (error) {
console.log('Request failed', error);
//redirección
window.location.replace("/Gerente.html")
});
//validación de usuario
}
};