In this simple app the unauthenticated user searches for restaurants with the city name, and when the user chooses a restaurant, he can click on going but he needs to login with twitter first,
now when the user is back after authentication I want to resubmit the term the user inserted so he won’t have to search again. this is what I tried
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')
};
This works but the problem is when the form is submitted automatically it redirects again to “http://localhost:3000/?location=new+york” which results in data not displaying.
I don’t know how to stop it from doing that.