JQuery Ajax Force update

I have a form that accepts user credentials. if the user has invalid user credentials, the page will not load unless he or she has valid credentials. I want the page to refresh the whole page, how would I achieve this? I tried to add.

window.location.href = window.location.href; 

      

In the html answer, but that didn't work, seems like jquery is removing the script code?

here's the AJAX for submitting the form.

$('body').on('submit', '#sign-in', function(e) {
    e.preventDefault();

    var data = $(this).serialize();

    var url = $(this).attr('action');
    $.ajax({
        //this is the php file that processes the data and send mail
        url : url,
        type : "POST",
        data : data,
        //Do not cache the page
        cache : false,
        //success
        success : function(data) {
            alert(data);
            $('#loginModal').html($(data).find('#loginModal').html());
        }
    });
})

      

if the user has valid credentials, the success response will be this

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script>
    window.location.href = window.location.href;

</script>
</head>

      

but the page doesn't reload.

+3


source to share


2 answers


In the success function, you can use the JavaScript way to reload:

location.reload();

      

A bit like



$('body').on('submit', '#sign-in', function(e) {
    e.preventDefault();

    var data = $(this).serialize();

    var url = $(this).attr('action');
    $.ajax({
        //this is the php file that processes the data and send mail
        url : url,
        type : "POST",
        data : data,
        //Do not cache the page
        cache : false,
        //login success
        success : function(data) {
            //... your other code
            location.reload(); //reload the page on the success
        }
    });
})

      

This means your request encounters an error on login failure, so it is not part of your success function.

+3


source


See if you can use this code in a successful function

On receipt of a valid response (e.g. 1)



$(document).attr('location').href='your location'

      

0


source







All Articles