简体   繁体   中英

Jquery Ajax Send to plugin action WordPress

I'm using ajax to send form data to a plugin action and I can't get the jQuery to link to the function. The jQuery seems to be sending the code perfectly as i can see via the headers. However the WordPress php function call isn't being actioned. Can't figure it out.

$.ajax({
        url: cjdAjax.ajaxurl,
        type: 'POST',
        action: 'cjd_send_test_email',
        data: {
            'email': email, 
            'subject': subject,
            'content': content
        },
        success: function( data ) {
            console.log( data );
            $(".test-box .spinner").hide();
            $(".test-email-message").slideDown();
        }

    });

PHP code

wp_localize_script( 'cjd_admin_script', 'cjdAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));

add_action( 'wp_ajax_cjd_send_test_email', 'cjd_send_email_test' );
add_action( 'wp_ajax_nopriv_cjd_send_test_email', 'cjd_send_email_test' );

function cjd_send_email_test(){
  echo $_POST['content'];
  echo $_POST['subject'];
  wp_die(); // ajax call must die to avoid trailing 0 in your response
}

The action param should be part of the data array:

data: {
    action: 'cjd_send_test_email',
    email: email, 
    subject: subject,
    content: content
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM