This question already has an answer here:

When I use this $form_state->setResponse(new TrustedRedirectResponse($url, 302)); in submitForm() it works fine. But when I try to use it in jQuery callback function it doesn't redirect me. How to make it work?

marked as duplicate by Clive 8 May 18 at 16:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Welcome to Drupal Answers! It's not clear what you mean, since you show PHP code and say that it doesn't work in a jQuery callback. PHP code is totally different from JavaScript code; you cannot take PHP code (especially code involving Drupal classes) and expect it works as it is in JavaScript. – kiamlaluno May 18 at 15:58

You can't. The 302 response code will only redirect your AJAX request, not the browser page executing it. You'll need to your AJAX callback function implement the redirect in JS code instead.

Not the answer you're looking for? Browse other questions tagged or ask your own question.