【问题标题】:Wordpress Ajax JSON success return no being recognizedWordpress Ajax JSON 成功返回不被识别
【发布时间】:2015-10-25 03:21:15
【问题描述】:

我正在使用 jQuery Ajax 调用方法为网站制作联系表单,然后使用 Wordpress 在 admin-ajax.php 中的构建提交表单的值,通过 wp_mail 将它们发送到电子邮件地址,然后,如果成功,发送通过 json_encode 返回一个数组。表单有效,电子邮件发送,但成功数据未发送,Ajax :success 函数未启动。这是我一直在使用的。

jQuery

(document).ready(function($){

$.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-z\s]+$/i.test(value);
}, "Only alphabetical characters"); 

$.validator.addMethod("phoneNum", function(value, element) {
    return this.optional(element) || /^[0-9\-\s]+$/i.test(value);
}, "Phone number can only be number and dashes.");


$("#contact-form").validate({
    rules: {
        name: {
            required:true,
           lettersonly: true
        },
        email: {
            required: true,
            email: true
            },
        phone: {
            phoneNum: true,
            },
        message:  {
            required:true
        }
        },
     messages: {
            name:  {
                required: "Please enter your name.",
                lettersonly: "Needs to be letters, no numbers please."
            },
            email: {
                required: "Please enter your email adress.",
                email: "Please enter your valid email adress."
            },
            phone: {

            },
            message:{
                required: "Please enter a message.",
            }
          },

    submitHandler: function(form) {
    $('#contact-msg').html('<p class="ajaxLoader">Sending Email...</p>');

       $.ajax ({
            type: 'POST',
            url:  ajax_object.ajax_url,
            data: $('#contact-form').serialize(),
            dataType: 'json',
            success: function(response) {
                        if (response.status == 'success') {
                            $('#contact-form')[0].reset();
                        }
                        $('#contact-msg').html(response.errmessage);
                    }
                });

    }

});
});

函数.php

// CONTACT FORM SCRIPTS    
function contactform_add_script() {

wp_enqueue_script( 'contactform-script', get_template_directory_uri().'/assets/js/contact_me.js', array('jquery') , null, true);
wp_localize_script( 'contactform-script', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );

}
add_action('wp_enqueue_scripts', 'contactform_add_script');


// CONTACT FORM PROCESSING 
 function ajax_contactform_action_callback() {
$error = '';
$status = 'error';
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) {
    $error = 'All fields are required to enter.';

    } else {
        $name = filter_var($_POST['name'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
        $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
        $number = filter_var($_POST['phone'], FILTER_SANITIZE_NUMBER_INT);
       // $treatments = filter_var($_POST['treatments'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
        $subject = 'A message from St. Romain Interiors\'s contact form.';
        $message .= PHP_EOL.'Sender\'s name: '.$name;
        $message .= PHP_EOL.'Phone number: '.$number;
        $message .= PHP_EOL.'E-mail address: '.$email;

        $message .= PHP_EOL.'Message: '.stripslashes($_POST['message']);

        $sendmsg = "Thanks for the message! We will respond as soon as possible.";
        $to = 'blah@gmail.com'; // If you like change this email address
        // replace "noreply@yourdomain.com" with your real email address

        $header .= 'Reply-To: '.$email.PHP_EOL;
        if ( wp_mail($to, $subject, $message, $header) ) {
            $status = 'success';
            $error = $sendmsg;
        } else {
            $error = 'Some errors occurred.';
        }
    }

$resp = array('status' => $status, 'errmessage' => $error);
header( "Content-Type: application/json" );
echo json_encode($resp);
die();
}
add_action( 'wp_ajax_contactform_action', 'ajax_contactform_action_callback' );
add_action( 'wp_ajax_nopriv_contactform_action',      'ajax_contactform_action_callback' );

这已在其他网站上运行,我不确定为什么它在此站点上不起作用。它发送电子邮件,只是我的 jQuery 方法没有成功回调。

更新

我下载了 Firebug,这是我在提交表单时得到的回复。

<br />
<b>Notice</b>:  Undefined index: name in <b>/home/theski/public_html/stromain/wp-content/themes/stromain
/functions.php</b> on line <b>156</b><br />
<br />
<b>Notice</b>:  Undefined index: email in <b>/home/theski/public_html/stromain/wp-content/themes/stromain
/functions.php</b> on line <b>157</b><br />
<br />
<b>Notice</b>:  Undefined index: phone in <b>/home/theski/public_html/stromain/wp-content/themes/stromain
/functions.php</b> on line <b>158</b><br />
<br />
<b>Notice</b>:  Undefined variable: message in<b>/home/theski/public_html/stromain/wp-content/themes
/stromain/functions.php</b> on line <b>161</b><br />
<br />
<b>Notice</b>:  Undefined index: message in <b>/home/theski/public_html/stromain/wp-content/themes/stromain
/functions.php</b> on line <b>165</b><br />
<br />
<b>Notice</b>:  Undefined variable: header in  <b>/home/theski/public_html/stromain/wp-content/themes
/stromain/functions.php</b> on line <b>171</b><br />
{"status":"success","errmessage":"Thanks for the message! We will respond as soon as possible."}

【问题讨论】:

  • 你试过用firebug的控制台(firefox扩展)调试它吗?
  • 嗨 Jenis,感谢您的评论,我用 Firebug 中的回复更新了上面的帖子。

标签: json ajax wordpress


【解决方案1】:

答案是 wp_config 中的 debug 设置为“true”。这创建了由 Wordpress 返回的错误 html,因为 functions.php 中的变量是通过 ajax 分配的,但 Wordpress 调试将它们报告为“未定义”。我通过在表单上提交内容时查看 Firebug 控制台选项卡上的返回信息发现了这一点。返回充满了
和未定义的错误。这导致 AJAX 响应中断,不允许我激活 ajax 回调。将 wp_config debug 设置为 'false' 可以解决所有问题。

至少这是我的假设,如果我错了,请纠正我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多