WordPress AJAX成功响应

时间:2014-10-16 作者:user51442

我正在尝试使用wp\\U mail和ajax在Wordpress中创建一个联系人表单

emial工作正常,但我对ajax的成功响应有问题。

在电子邮件发送后,我需要向用户显示一条消息,说明电子邮件已发送。

js验证表单并显示成功消息。

    $atj(function(){
      $atj(\'#training-submit\').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        if(verfiyFields()) {
            alert(\'here\');
            requestData = {
              \'action\' : \'myajax-submit\',
              \'firstName\' : $atj("#name").val(), 
              \'email\' : $atj("#email").val(), 
          }
          $atj.post(MyAjax.ajaxurl, requestData).done(function(result){        
            result = jQuery.parseJSON( result );
            console.log(result);
            if(result == \'success\'){
              $atj(\'.training-form [type=text]\').val(\'\');
              $atj(\'.training-form-message\').append(\'<p class="training-form-complete-message">Thank you for the  email</p>\');
            }
          });
        }
      });
    })



    //Verfiy 
    function verfiyTrainingFields() {
      var flag = true;

      var name =        $atj(\'#name\');
      var email =       $atj(\'#email\');

      if(name.val().indexOf(\' \') === -1 ){
        name.parent().prepend(\'<p class="form-error">Please enter name, first space last</p>\');
        errorMessage($atj(\'.form-error\'));
        flag = false;
      }
      if(!IsEmail(email.val())){
        email.parent().prepend(\'<p class="form-error">Please enter valid email address</p>\');
        errorMessage($atj(\'.form-error\'));
        flag = false;
      }
      return flag;
    }
功能。php文件发送电子邮件并发送ajax响应。

    function myajax_submit() {

        $name = sanitize_text_field($_POST[\'firstName\']);
        $email = sanitize_text_field($_POST[\'email\']);

        $headers[] = \'From: \' . $name . \' <\' . $email . \'>\' . "\\r\\n";
        $headers[] = \'Content-type: text/html\' . "\\r\\n"; //Enables HTML ContentType. Remove it for Plain Text Messages

        $to = \'[email protected]\';

        $message = \'Name: \' . $name . "\\r\\n" . \'email: \' . $email;

        add_filter( \'wp_mail_content_type\', \'set_html_content_type\' );
        wp_mail( $to, \'Email Test\', $message );
        remove_filter( \'wp_mail_content_type\', \'set_html_content_type\' ); 

        echo \'email sent\';

        // generate the response
        $response = json_encode( array( \'success\') );

        // response output
        header( "Content-Type: application/json" );
        echo $response;

        exit;
    }
电子邮件已发送,“email sent”回显会触发,但js中的if(result==“success”){不起作用。

控制台。js文件中的log(result)给出了以下内容。

    <br />
    <b>Warning</b>:  call_user_func_array() expects parameter 1 to be a valid callback, function \'set_html_content_type\' not found or invalid function name in <b>/Users/user.name/Desktop/MAMP-DocRoot/appname/src/php/wp-includes/plugin.php</b> on line <b>192</b><br />
    email sent<br />
    <b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /Users/user.name/Desktop/MAMP-DocRoot/appname/src/php/wp-includes/plugin.php:192) in <b>/Users/user.name/Desktop/MAMP-DocRoot/appname/src/php/wp-content/themes/sitename/functions.php</b> on line <b>66</b><br />
    ["success"] 

1 个回复
SO网友:shanebp

对于ajax响应,不能使用2个回声。

尝试以下操作:

js公司:

  $atj.post(MyAjax.ajaxurl, requestData).done(function(result){        
    console.log(result);
    if(result == \'success\'){
      $atj(\'.training-form [type=text]\').val(\'\');
      $atj(\'.training-form-message\').append(\'<p class="training-form-complete-message">Thank you for the  email</p>\');
    }
  });
php

function myajax_submit() {
// snip...

 $mail_check = wp_mail( $to, \'Email Test\', $message );
 remove_filter( \'wp_mail_content_type\', \'set_html_content_type\' ); 

 if( $mail_check) 
   echo \'success\';
 else
   echo \'failure\';
 exit;
}

结束

相关推荐

AJAX-SHORTINIT设置为TRUE将返回空白

我想我会this nifty shortinit trick 转一圈。然而,当我在自定义ajax处理程序中包含此内容时,我的回调没有得到任何响应。如果我对它进行评论,它的效果很好。有什么变化吗?<?php //mimic the actual admin-ajax define(\'DOING_AJAX\', true); if (!isset( $_POST[\'action\'])) die(\'-1\'); ini_set(\