在这个简单的应用程序中,未经身份验证的用户搜索具有城市名称的餐馆, 当用户选择一家餐馆时,他可以点击进入,但他需要先用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" 这导致数据无法显示。
我不知道如何阻止它这样做。
答案 0 :(得分:0)
如果你没有指定
,我认为(我不是xhr的专家)window.location //or
window.redirect
您的浏览器“重新加载”您拥有的最后一个网址。当您自动提交时,请尝试将重定向添加到所需的链接。