Ajax Error: Unexpected token < in JSON at position 0 on WordPress Contact Form 7 submit
Ajax Error: Unexpected token < in JSON at position 0 on WordPress Contact Form 7 submit
我在发布联系表单时遇到了一个奇怪的问题。
加载图标一直在加载,表单没有提交。
电子邮件已发送,我的 before_send_mail 功能也有效。奇怪的是,当我取消注释 before_send_mail 函数时,它没有显示任何错误。所以它可能来自我的代码。
但是首页没有改变状态,一直显示加载图标。
错误信息是:
<div class="ajax-error">Unexpected token < 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 指点我。
@Senta 的回答帮我找到了错误的原因。事实证明,CF7 的 AJAX 调用收到的响应不仅仅是来自 CF7 的 JSON 响应,而是另一个插件正在向其中添加内容,导致 AJAX 失败,因为它期待 json 格式,现在非常无效(原本应该是 JSON 的 1 行变成了 300 多行其他内容)。
我不喜欢编辑插件,但我这样做是为了帮助解决这个问题并让 CF7 与其他人一起玩,现在它看起来像这样:
contact-form-7/includes/js/scripts.js 或者,如果您正在使用 Contact Form 7 的 jQuery 验证插件,您需要 jquery-validation-for-contact-form-7/js/jquery.jvcf7_validation.js
$.ajax({
type: 'POST',
url: wpcf7.apiSettings.root + wpcf7.apiSettings.namespace +
'/contact-forms/' + wpcf7.getId($form) + '/feedback',
data: formData,
dataType: 'text',//Changed from 'json' to 'text'
processData: false,
contentType: false
}).done(function(data, status, xhr) {
var f = data.indexOf('{'); //First opening curly-brace
var l = data.lastIndexOf('{'); //Last opening curly-brace
if (f !== l && data.indexOf("{") !== 0 && l !== 0) {
//If the first and last indices are not the same, and neither the first nor the last are equal to 0,
//then get the data that starts from the last index
data = data.substring(l);
} else if (f === l && f !== 0) {
//If the first and last indices are the same, but are not the first character,
//then get the data that starts from the first index
data = data.substring(f);
}
$('.ajax-loader', $form).removeClass('is-active');
try {
//Try parsing the data as JSON now and submitting it
data = JSON.parse(data);
ajaxSuccess(data, status, xhr, $form);
} catch (err) {
//Do the same error stuff as the fail() call if it still fails
var $e = $('<div class="ajax-error"</div>').text(err);
$form.after($e);
}
}).fail(function(xhr, status, error) {
$('.ajax-loader', $form).removeClass('is-active');
var $e = $('<div class="ajax-error"></div>').text(error.message);
$form.after($e);
});
我不容忍编辑其他开发人员的插件,这可能不适用于遇到此问题的每个人。这只是一个临时修复,直到我们找到一个可以很好地与 CF7 配合使用的不同插件。
编辑:
今天又遇到了这个错误。事实证明,在 wp-config.php 设置中将 WP_DEBUG 设置为 false 也可以解决此错误,因为 AJAX 响应前面的内容是一堆调试日志。如果您实际调试它会很有帮助,否则我会在您的生产环境中将其关闭。
我在发布联系表单时遇到了一个奇怪的问题。
加载图标一直在加载,表单没有提交。
电子邮件已发送,我的 before_send_mail 功能也有效。奇怪的是,当我取消注释 before_send_mail 函数时,它没有显示任何错误。所以它可能来自我的代码。
但是首页没有改变状态,一直显示加载图标。
错误信息是:
<div class="ajax-error">Unexpected token < 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 指点我。
@Senta 的回答帮我找到了错误的原因。事实证明,CF7 的 AJAX 调用收到的响应不仅仅是来自 CF7 的 JSON 响应,而是另一个插件正在向其中添加内容,导致 AJAX 失败,因为它期待 json 格式,现在非常无效(原本应该是 JSON 的 1 行变成了 300 多行其他内容)。
我不喜欢编辑插件,但我这样做是为了帮助解决这个问题并让 CF7 与其他人一起玩,现在它看起来像这样:
contact-form-7/includes/js/scripts.js 或者,如果您正在使用 Contact Form 7 的 jQuery 验证插件,您需要 jquery-validation-for-contact-form-7/js/jquery.jvcf7_validation.js
$.ajax({
type: 'POST',
url: wpcf7.apiSettings.root + wpcf7.apiSettings.namespace +
'/contact-forms/' + wpcf7.getId($form) + '/feedback',
data: formData,
dataType: 'text',//Changed from 'json' to 'text'
processData: false,
contentType: false
}).done(function(data, status, xhr) {
var f = data.indexOf('{'); //First opening curly-brace
var l = data.lastIndexOf('{'); //Last opening curly-brace
if (f !== l && data.indexOf("{") !== 0 && l !== 0) {
//If the first and last indices are not the same, and neither the first nor the last are equal to 0,
//then get the data that starts from the last index
data = data.substring(l);
} else if (f === l && f !== 0) {
//If the first and last indices are the same, but are not the first character,
//then get the data that starts from the first index
data = data.substring(f);
}
$('.ajax-loader', $form).removeClass('is-active');
try {
//Try parsing the data as JSON now and submitting it
data = JSON.parse(data);
ajaxSuccess(data, status, xhr, $form);
} catch (err) {
//Do the same error stuff as the fail() call if it still fails
var $e = $('<div class="ajax-error"</div>').text(err);
$form.after($e);
}
}).fail(function(xhr, status, error) {
$('.ajax-loader', $form).removeClass('is-active');
var $e = $('<div class="ajax-error"></div>').text(error.message);
$form.after($e);
});
我不容忍编辑其他开发人员的插件,这可能不适用于遇到此问题的每个人。这只是一个临时修复,直到我们找到一个可以很好地与 CF7 配合使用的不同插件。
编辑:
今天又遇到了这个错误。事实证明,在 wp-config.php 设置中将 WP_DEBUG 设置为 false 也可以解决此错误,因为 AJAX 响应前面的内容是一堆调试日志。如果您实际调试它会很有帮助,否则我会在您的生产环境中将其关闭。