Jack Moscovi
Jack Moscovi

Reputation: 2269

How do redirect in Ajax Post?

Im trying to redirect Ajax post in a success function

Here's the code

$.ajax({
    type: "POST",
    url: "/api/create-room/",
    data:{
      targetedUser,
    },
    success: function(data) {
      // How to redirect in here??
    }
});

Upvotes: 1

Views: 45

Answers (1)

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

You could use window.location.replace or window.location.href inside the success function :

$.ajax({
    type: "POST",
    url: "/api/create-room/",
    data:{
        targetedUser,
    },
    success: function(data) {
        window.location.replace("url"); //Simulate HTTP redirection
        //Or
        window.location.href = "url"; //Simulate click on a link
    }
});

For more information you could check This post.

Hope this helps.

Upvotes: 3

Related Questions