0

我知道我的问题被问了很多次,我阅读了所有关于这个问题的讨论,但我没有找到正确的答案。

我的问题是总是在 wordpress 上调用返回的admin-ajax请求。我的代码是:ajax0

前端调用

$.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 发送它。我用服务器电子邮件在本地主机上尝试了所有这些,一切都很完美。有人可以帮我解决它吗?谢谢

4

2 回答 2

0

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

于 2017-06-10T09:22:15.373 回答
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)

于 2017-06-08T15:48:19.200 回答