user3147424
user3147424

Reputation: 3682

Redirect URL after AJAX in JavaScript / jQuery

I have an ajax call and am trying to redirect my page after it makes the call to another URL. However, it just refreshes the page. What can I do to redirect my page?

<button onclick="SignUp()" class="btn btn-success text-center">Submit Information</button>

function SignUp() {

    var first_name = document.getElementById('first_name').value;
    var last_name = document.getElementById('last_name').value;

    $.ajax({
        type: 'POST',
        url: 'signup.php',
        data: { "first_name": first_name, "last_name": last_name },
        async: false,
        success: (function () {
            window.location.href = "http://stackoverflow.com";
        })
    });
}

Upvotes: 1

Views: 4396

Answers (3)

Satpal
Satpal

Reputation: 133453

Use

  window.location.href = "http://stackoverflow.com";

EDIT

As button is in form its default behavior is submit

So you have to add type="button" to button like

<button type="button" >Submit Information</button>

OR

You need to use return false

HTML

<button onclick="return SignUp()" >Submit Information</button>

JavaScript

function SignUp() {
    //Your code  
        return false;        
};

Upvotes: 2

pathfinder
pathfinder

Reputation: 1786

Since your button is in a form you need to add this to onClick or the page refreshes due to a form submit.

<button onclick="SignUp()"; return false" ...

Upvotes: 0

Quentin
Quentin

Reputation: 944320

Your submit button is submitting the form before the success handler is processed.

Add type="button" so it isn't a submit button.

Upvotes: 1

Related Questions