如何使用javascript重定向?
如您所见,我使用的登录表单在线验证用户和密码,生成令牌,并在令牌存在后将用户重定向到如何使用javascript重定向?,javascript,html,token,window.location,Javascript,Html,Token,Window.location,如您所见,我使用的登录表单在线验证用户和密码,生成令牌,并在令牌存在后将用户重定向到pacientes.html页面 问题从renderApp开始。它确实执行window.location.href,但为了重定向,我必须手动刷新页面。我做错什么了吗 let ruta = 'login' const renderApp = () =>{ const token = localStorage.getItem('token') if (token) { wind
pacientes.html
页面
问题从renderApp
开始。它确实执行window.location.href
,但为了重定向,我必须手动刷新页面。我做错什么了吗
let ruta = 'login'
const renderApp = () =>{
const token = localStorage.getItem('token')
if (token) {
window.location.href = "pacientes.html"
}
}
window.onload= () => {
renderApp()
const loginForm= document.getElementById('login')
loginForm.onsubmit = (e) =>{
e.preventDefault()
e.stopPropagation()
const cedula = document.getElementById('cedula').value
const password = document.getElementById('password').value
fetch('https://janfa.gharsnull.now.sh/api/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({cedula, password})
}).then( x => x.json())
.then( respuesta => {
localStorage.setItem('token' , respuesta.token)
ruta = 'users'
})
}
}
如果要在收到HTTP请求的响应时重定向,请将要重定向的代码放入
然后回调中
i、 e.将ruta='users'
替换为location.href=“pacientes.html”
如果你把它放在渲染函数中,它只会在组件渲染时执行 所以url变了,但页面没有重新加载?哦,上帝,非常感谢你。我只是在学习js,这种事情对我来说很难