我正在努力想办法解决这个问题。我在这里遵循了针对AJAX的wordpress codex:http://codex.wordpress.org/AJAX_in_Plugins
我一直返回的是0,我从来没有得到1。知道为什么吗?我的代码如下。
PHP:
add_action(\'wp_enqueue_scripts\', \'live_validation\' );
add_action(\'wp_ajax_validate_email\', \'validate_email_input\');
add_action(\'wp_ajax_nopriv_validate_email\', \'validate_email_input\');
function live_validation() {
wp_enqueue_script( "validate_email", STRIPE_BASE_URL . \'inc/js/check-email.js\', array( \'jquery\' ) );
wp_localize_script( "validate_email", "validateEmail", array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );
}
function validate_email_input() {
$email = $_POST[\'email\'];
if ( email_exists($email) ) {
echo 1;
} else {
echo 0;
}
exit;
}
JS公司:
jQuery(document).ready(function($) {
$(\'input[name=email]\').keyup(function() {
var input_value = $(this).val();
$.post( validateEmail.ajaxurl, { action:\'validate_email\', email:input_value }, function(data) {
$(\'.message\').html(data);
});
});
});
HTML:
<div class="form-row">
<label for="email"><?php _e(\'Email\', \'oe\'); ?></label>
<input type="text" size="20" id="email" name="email" />
<span class="message"></span>
</div>