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
  • 65,304
  • 15
  • 70
  • 95
Jack Moscovi
  • 2,025
  • 5
  • 26
  • 48

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
  • 65,304
  • 15
  • 70
  • 95