wordpress admin-ajax总是返回0

时间:2017-06-08 15:34:52

标签: php jquery wordpress phpmailer admin-ajax

我知道我的问题很多时候都被问到了,我读了所有关于这个问题的讨论,但我找不到合适的答案。

我的问题是admin-ajax请求ajax来自{word}} 0。 我的代码是:

前端通话

$.ajax({
   url: cubetechform_ajax.ajax_url,
   type: "POST",
    data: {
        action: 'cubetech_contact_form_send_email',
        name: name,
        phone: phone,
        email: email,
        message: message
    },
    cache: false,
    success: function(response) {
        console.log(response);
    },
    error: function(XMLHttpRequest, ajaxOptions, thrownError) {                     
        //console.log(XMLHttpRequest.status + '<br/>' + ajaxOptions + '<br/>' + thrownError);               
    },
});

后端

/*
 * Action hooks
 */
function __construct() {

    // Enqueue plugin styles and scripts
    add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_cubetech_form_scripts' ) );

    // Setup Ajax action hook
    add_action( 'wp_ajax_cubetech_send_email', array( $this, 'cubetech_send_email' ) );
    add_action( 'wp_ajax_nopriv_cubetech_send_email', array( $this, 'cubetech_send_email' ) );

    //$this->cubetech_send_email();
}

/**
 * Register plugin styles and scripts
 */
public function register_contactme_scripts() {
    wp_register_script( 'contactme_script', get_template_directory_uri() . '/js/contact_me.js', array('jquery','bootstrapValidationScript'), '', true);
}   
/**
 * Enqueues plugin-specific scripts.
 */
public function enqueue_cubetech_form_scripts() {       
    wp_enqueue_script( 'contactme_script', plugins_url( '/js/contact_me.js', __FILE__ ), array('jquery','bootstrapValidationScript'), '', true);
    //wp_enqueue_script( 'contactme-script' );
    wp_localize_script( 'contactme_script', 'cubetechform_ajax', array( 'ajax_url' => admin_url('admin-ajax.php')) );
}   


public function cubetech_send_email() { 
    require_once ABSPATH . WPINC . '/class-phpmailer.php'; 
    require_once ABSPATH . WPINC . '/class-smtp.php';

    $name = strip_tags(htmlspecialchars($_POST['name']));
    $email_address = strip_tags(htmlspecialchars($_POST['email']));
    $phone = strip_tags(htmlspecialchars($_POST['phone']));
    $message = strip_tags(htmlspecialchars($_POST['message']));

    // definisco il messaggio formattato in HTML
    $email_body = '<html><body>';
    $email_body .= '<p>Nome: '.$name.'<br>';
    $email_body .= 'N. Telefono: '.$phone.'<br>';
    $email_body .= 'Richiesta: '.$message.'</p>'.'<br>';
    $email_body .= '</body></html>';

    // definisco il messaggio formattato in HTML
    $email_body = '<html><body>';
    $email_body .= '<p>Nome: '.$name.'<br>';
    $email_body .= 'N. Telefono: '.$phone.'<br>';
    $email_body .= 'Richiesta: '.$message.'</p>'.'<br>';
    $email_body .= '</body></html>';

    $to = get_option('admin_email');
    $subject = "Richiesta da: ". $email_address;

    $mail = new PHPMailer();

    $mail->IsSMTP(); // set mailer to use SMTP
    $mail->Host = "smtps.aruba.it";  // specify main and backup server
    //$mail->SMTPDebug = 3;  // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true;     // turn on SMTP authentication
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;
    $mail->Username = "info@cubetech.it";  // SMTP username
    $mail->Password = "*******"; // SMTP password

    $mail->From = "info@cubetech.it";
    $mail->FromName = "Cube Tech Bologna";
    $mail->AddAddress("info@cubetech.it", "Cube Tech Bologna" );  // name is optional
    $mail->AddReplyTo("info@cubetech.it", "Reply");
    $mail->AddCC($email_address); 

    $mail->IsHTML(true);  // set email format to HTML

    $mail->SetFrom("info@cubetech.it", "Cube Tech Bologna");
    $mail->Subject = $subject;
    $mail->Body = $email_body;

    $mail->AddAddress("info@cubetech.it");

    $error = '';

    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        echo $error;
        exit();
        die();
    } else {
        $error = 'ok';
        echo $error;
        exit();
        die();
    }
}

我尝试了所有关于die(); exit(); $mail = new PHPMailer(); $mail = new PHPMailer(true);以及其他许多内容的内容。问题始终是ajax调用,因为如果我尝试直接调用函数cubetech_send_email()它完美地工作并返回ok并且phpmailer发送它。我尝试使用服务器电子邮件在localhost上完成所有这一切,所有工作都很完美..有人可以帮我解决吗?谢谢

2 个答案:

答案 0 :(得分:0)

你的钩子应该是这样的:

// Setup Ajax action hook
add_action( 'wp_ajax_cubetech_contact_form_send_email', array( $this, 'cubetech_send_email' ) );
add_action( 'wp_ajax_nopriv_cubetech_contact_form_send_email', array( $this, 'cubetech_send_email' ) );

在您的数据对象中,当执行ajax时,将“cubetech_contact_form_send_email”作为操作。钩子的工作方式如下:wp_ajax_(action) https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)

答案 1 :(得分:0)

经过很多天,我尝试了所有的事情...每个代码,我已经阅读了所有帖子和解决方案,我找到了解决方案......代码中没有任何错误......一切都很好。问题是当我将localhost wordpress传递给托管时。我不知道为什么,但也许在这个过程中没有顺利...我擦除主机上的数据库和目录并重新安装所有内容......我希望这可以帮助有这个问题的用户