1 回答

TA貢獻1818條經驗 獲得超8個贊
我不知道函數和選項是什么,但是如果您要將其替換為函數,則可以將屬性設置為在服務器告訴它時重定向。postfetchredirect: 'follow'
const Login = document.querySelector('.Login')
Login.addEventListener('submit', (e) => {
e.preventDefault()
const username = Login.querySelector('.username').value
const password = Login.querySelector('.password').value
fetch('/login/login', {
method: 'POST',
body: JSON.stringify({ username, password }),
redirect: 'follow'
}).then(({ status }) => {
if (status !== 200) alert('login failed')
})
})
或者,您可以從服務器發送應重定向到的 URL,然后從客戶端手動重定向到該頁面。
// Instead of res.redirect(), use send to send the URL back to the client.
res.send('/backend');
const Login = document.querySelector('.Login')
Login.addEventListener('submit', (e) => {
e.preventDefault()
const username = Login.querySelector('.username').value
const password = Login.querySelector('.password').value
post('/login/login', { username, password })
.then((response) => {
if (response.status === 200) {
alert('login success');
return response.text();
}
else alert('login failed')
})
.then((url) => {
location.replace(location.origin + url);
});
})
添加回答
舉報