Ajax Error: Unexpected token < in JSON at position 0 when submitting contact form 7 with before_send function

I have a weird issue when posting an contact form.

The loading icon keeps loading and the form does not submit.

The emails are sended and my before_send_mail functions also works. The weird thing is when i uncomment the before_send_mail function, it shows no errors. So it’s probably something from my code.

However the frontpage is not changing status and keeps showing the loading icon.

The error message sais:

<div class="ajax-error">Unexpected token &lt; in JSON at position 0</div>

This happens when I submit the form.

Can you guys help me out? Below you’ll find the before_send function.

add_action( 'wpcf7_before_send_mail', 'form_to_crm' );
function form_to_crm( $cf7 ) {
$wpcf7 = WPCF7_ContactForm::get_current();

/* Uw naam   => first_name    */ $first_name            = $_POST["your-name"];
/* Bedrijf   => company_name  */ $company               = $_POST["bedrijf"];
/* Email     => email         */ $email                 = $_POST["email"];
/* Adres     => address       */ $address               = $_POST["adres"];
/* Nummer*   => number        */ $number                = $_POST["huisnummer"];
/* Postcode  => postcode      */ $postcode              = $_POST["postcode"];
/* Woonplts* => city          */ $city                  = $_POST["woonplaats"];
/* Tel       => telephone     */ $telephone             = $_POST["telefoonnummer"]; 

if(!empty( $first_name )){          $post_items['first_name']           =  $first_name; }
if(!empty( $company )){             $post_items['company_name']         =  $company; }
if(!empty( $email )){               $post_items['email']                =  $email; }
if(!empty( $address )){             $post_items['address']              =  $address; }
if(!empty( $number )){              $post_items['number']               =  $number; }
if(!empty( $postcode )){            $post_items['postcode']             =  $postcode; }
if(!empty( $city )){                $post_items['city']                 =  $city; }
if(!empty( $telephone )){           $post_items['telephone']            =  $telephone; }

if(!empty($postcode) && !empty($number))
{
    $ch             = curl_init();

    if ( curl_error($ch) != "" ) 
    {
        return;
    }

    $post_string    = json_encode($post_items);

    $con_url        = 'valid api url';

    curl_setopt($ch, CURLOPT_URL, $con_url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Content-Type: application/json",
        "Authorization: Token XXX (censored)"
    ));
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_string);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

    $output = curl_exec($ch);

    file_put_contents("curlerror.txt", $output);
    curl_close($ch);
}
return $output;
}

Read more here: Ajax Error: Unexpected token < in JSON at position 0 when submitting contact form 7 with before_send function

Leave a Reply

Your email address will not be published. Required fields are marked *