重新加载后重新提交表单不起作用

问题描述 投票:-1回答:1

在这个简单的应用程序中,未经身份验证的用户搜索具有城市名称的餐馆,当用户选择餐馆时,他可以点击进入,但他需要先使用Twitter登录,

现在,当用户在身份验证后返回时,我想重新提交用户插入的术语,这样他就不必再次搜索了。这就是我的尝试

form.addEventListener('submit', (e) => {
 e.preventDefault();
 const xhttp = new XMLHttpRequest();
 xhttp.onreadystatechange = function(){

 if(this.readyState == 4 && this.status == 200) {

   //format the data for the user interface
   const businesses = JSON.parse(xhttp.responseText);
   formatData(businesses);

   }

 }
 xhttp.open("post", "http://localhost:3000/api/", true);
 xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 xhttp.send(`location=${input.value}`);

 //save searched term in a session
 sessionStorage.setItem('autosave', input.value);
 sessionStorage.setItem('refreshed', false);
});

//
//if the term is saved and the page is refreshed
//(will happen only when page is refreshed)
if (sessionStorage.getItem("autosave") && sessionStorage.getItem("false") == 'false') {
 //restore the contents of the text field
 input.value = sessionStorage.getItem("autosave");
 //then submit
 document.forms["myForm"].submit();
}else{
 console.log('no item')
};

这有效,但问题是当表单自动提交时,它会再次重定向到“http://localhost:3000/?location=new+york”,导致数据无法显示。

我不知道如何阻止它这样做。

javascript forms xmlhttprequest
1个回答
0
投票

如果你没有指明,我认为(我不是xhr的专家)

window.location //or
window.redirect

您的浏览器“重新加载”您拥有的最后一个网址。当您自动提交时,请尝试将重定向添加到所需的链接。

© www.soinside.com 2019 - 2024. All rights reserved.