Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Ajax错误:意外的令牌<;使用before_send函数提交联系人表单7时,在JSON位置0处_Php_Wordpress_Contact Form 7 - Fatal编程技术网

Php Ajax错误:意外的令牌<;使用before_send函数提交联系人表单7时,在JSON位置0处

Php Ajax错误:意外的令牌<;使用before_send函数提交联系人表单7时,在JSON位置0处,php,wordpress,contact-form-7,Php,Wordpress,Contact Form 7,我在发布联系人表单时遇到了一个奇怪的问题 加载图标会继续加载,表单不会提交 电子邮件被发送,我的“发送前邮件”功能也能正常工作。奇怪的是,当我取消注释before_send_mail函数时,它没有显示任何错误。所以这可能是我的代码中的一些东西 但是,frontpage不会更改状态,并始终显示加载图标 错误消息sais: <div class="ajax-error">Unexpected token &lt; in JSON at position 0</div>

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

加载图标会继续加载,表单不会提交

电子邮件被发送,我的“发送前邮件”功能也能正常工作。奇怪的是,当我取消注释before_send_mail函数时,它没有显示任何错误。所以这可能是我的代码中的一些东西

但是,frontpage不会更改状态,并始终显示加载图标

错误消息sais:

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

是的,我找到了答案。非常愚蠢。 在开发工具网络上,当我点击发送的json请求时,出现了一个完整的错误消息,显示我从联系人表单复制了一个不正确的变量名

我复制了[电子邮件],而它本应是[你的电子邮件]


希望它能帮到你

使用开发工具查看从AJAX请求返回的实际数据。有可能会出现一条HTML错误消息,告诉您出了什么问题。您可以在问题中向我们显示返回JSON响应吗?我将函数编辑为仅返回;不要返回输出。但是,返回显示以下消息:{“success”:“Person saved”}
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;
}