发布联系表格时我有一个奇怪的问题。
加载图标会继续加载,表单不会提交。
发送电子邮件,我的before_send_mail功能也可以使用。奇怪的是当我取消注释before_send_mail函数时,它没有显示任何错误。所以它可能来自我的代码。
然而,首页不会改变状态并继续显示加载图标。
错误消息sais:
<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;
}
答案 0 :(得分:2)
在浏览器控制台中,ajax调用未列为错误。但是在通话中有一个错误。感谢@JAAulde指点我。
答案 1 :(得分:2)
我不喜欢编辑插件,但我这样做是为了帮助解决这个问题,让CF7与其他人一起玩得很好,现在看起来像这样:
contact-form-7 / includes / js / scripts.js或者如果您使用的是联系表格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响应的内容是一堆调试日志。如果您实际上正在调试它,那将很有帮助,但除此之外,我会将其保留在您的生产环境中。