Asked 3 years ago
24 Jun 2021
Views 638
Lorna

Lorna posted

How to manage a redirect request after a jQuery Ajax call

How to manage a redirect request after a jQuery Ajax call
jaydeep

jaydeep
answered Apr 28 '23 00:00

After making an AJAX request with jQuery, you can redirect the user to a new page using the window. location property.

Here's an example of how to do this:



$.ajax({
  url: '/path/to/your/api',
  type: 'POST',
  data: { /* your data */ },
  success: function(response) {
    // Redirect the user to the new page
    window.location.href = '/path/to/new/page';
  },
  error: function(jqXHR, textStatus, errorThrown) {
    // Handle error case here
  }
});

In this example, the AJAX request is sent to a server-side API located at / path/to/your/api. I f the request is successful, the user is redirected to a new page located at /path/to/new/page using the window.location.href property. If the request fails, the error function is called instead.

You can customize the URL that the user is redirected to and the data that is sent with the AJAX request to fit your specific use case.
Post Answer