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

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

我在发布联系表单时遇到了一个奇怪的问题。

加载图标一直在加载,表单没有提交。

电子邮件已发送,我的 before_send_mail 功能也有效。奇怪的是,当我取消注释 before_send_mail 函数时,它没有显示任何错误。所以它可能来自我的代码。

但是首页没有改变状态,一直显示加载图标。

错误信息是:

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

当我提交表单时会发生这种情况。

你们能帮帮我吗?下面是 before_send 函数。

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;
}

是的,我确实找到了答案。太傻了。 在 Dev tools 网络上,当我点击我发送的 json 请求时,出现了一条完整的错误消息,显示我从我的联系表单中复制了一个不正确的变量名。

我复制了 [email] 而它应该是 [your-email]

希望对您有所帮助!