簡體   English   中英

Ajax錯誤:使用before_send函數提交聯系表單7時,JSON位置0的標記<<意外

[英]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;
}

是的,我確實找到了答案。 相當愚蠢的TBH。 在開發工具網絡上,當我單擊發送的json請求時,出現一個完整的錯誤消息,顯示我從聯系表單中復制了錯誤的變量名。

我復制了[電子郵件],但它本來應該是[您的電子郵件]

希望它可以幫助您!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM