How to redirect custom ajax form to external url When setting the form with ajax handler like this

  public function buildForm(array $form, FormStateInterface $form_state) {
    //...

    $form['actions']['submit'] = array(
      //...
      '#ajax' =&gt; [
        'wrapper' =&gt; 'custom_form_wrapper',
        'callback' =&gt; '::ajaxCallback',
      ],      
    );
  }    


and trying to set the redirect on submitForm 

  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
    $url = 'https://example.com';
    $form_state-&gt;setResponse(new \Drupal\Core\Routing\TrustedRedirectResponse($url, 302));
  }    


nothing happens and no error message appears but the form seems like it has finished processing.

When the ajax part is taken out the action:submit array the form redirects properly on submit.  What should be done differently ?
You can't return a redirect response, because the ajax script in the browser expects a json array with jquery commands, so you need to return a RedirectCommand from the ajax callback:

  public function ajaxCallback(array &amp;$form, FormStateInterface $form_state) {
    $response = new AjaxResponse();

    $command = new RedirectCommand('https://example.com');

    return $response-&gt;addCommand($command);
  }


which then assigns window.location to redirect client-side.