草庐IT

php - Ajax 错误 : Unexpected token < in JSON at position 0 on WordPress Contact Form 7 submit

coder 2024-04-30 原文

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

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

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

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

错误信息是这样的:

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

最佳答案

在浏览器控制台中,ajax 调用未列为错误。但是调用中出现错误。感谢@JAAulde 向我指出这一点。

关于php - Ajax 错误 : Unexpected token < in JSON at position 0 on WordPress Contact Form 7 submit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43683683/

有关php - Ajax 错误 : Unexpected token < in JSON at position 0 on WordPress Contact Form 7 submit的更多相关文章

随机推荐