soflyy / breakdance-bugs

Bug reports from Breakdance users.
40 stars 6 forks source link

CORS Error When Redirecting to External URL using Custom Form Action #979

Closed alvindcaesar closed 9 months ago

alvindcaesar commented 9 months ago

I am encountering a CORS warning issue in the browser console when attempting to redirect to an external URL using a custom form action. The redirection is initiated within the run() function.

Here's the scenario I'm facing:

  1. Upon submission, the form sends an API request to a third-party server.
  2. The third-party server responds with a URL.
  3. The intention is to redirect the user to the provided URL.

Below is the relevant code snippet that demonstrates the issue:

<?php

class MyFormCustomAction extends \Breakdance\Forms\Actions\ApiAction 
{
  public static function name() {
    return 'My Form Custom Action';
  }

  public static function slug() {
    return 'my-form-custom-action';
  }

  public function run($form, $settings, $extra) {
    $response = wp_remote_post( $endpoint, $args );

    $theURLtoredirect = json_decode( wp_remote_retrieve_body( $response ) );

    wp_redirect( $theURLtoredirect );

    exit;
  }
}

Screenshot of the error ( assumes the link https://example.com is the URL returned by the server ):

Screenshot 2023-10-30 144013

I am seeking guidance on the proper way to handle this situation and resolve the CORS error during the redirection process.