I'm learning Ajax form process. My problem is: When I submit a form it works and redirects to another page. But when I click the back button there, I don't get redirected to the previous page. What am I doing wrong here?
<form id="form-admin-login">
<input name="username" type="text" placeholder="User name" class="input-field" required="">
<input name="password" type="password" placeholder="Password" class="input-field" required="">
</form>
<button name="submit" onClick="submit_login()" class="btn btn-login">Login</button>
ajax
function submit_login(){
$.ajax({
type: "POST",
data: $("#form-admin-login").serialize(),
url: "ajax/ajax-admin-login.php",
dataType: "json",
cache: false,
beforeSend:function(){
},
success: function(data) {
if(data['error']==0)
{
window.location.replace('dashboard.php');
}
else if (data['error']==1)
{
$("#showError").removeClass("hide");
$("#error-message").html(data['message']);
}
},
error: function(jqXHR, textStatus, errorThrown){
}
})
You need to redirect with
window.location.assign(url);
window.location = url;
window.location.href = url;
not with
window.location.replace(URL);
because the location.href will simply navigate to the new URL. The replace method on the other hand navigates to the URL without adding a new record to the history.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.