1

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??
    }
});
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
Jack Moscovi
  • 2,195
  • 7
  • 30
  • 49

1 Answers1

3

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.

Community
  • 1
  • 1
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101