Codex tools: Log in
Contents |
Send a JSON response back to an Ajax request, indicating failure.
<?php wp_send_json_error( $data );
$data = array( 'success' => false ); //if $data is empty ?>
jQuery(document).ready(function(){
jQuery('#btn_save').click(function(e){
e.preventDefault();
jQuery.post(pluginUrl+'ajax/save_field.php',jQuery('#my-form').serialize(), function(data) {
alert(data.success);
});
});
});
save_field.php
<?php $nonce=$_POST['_wpnonce_name']; if (empty($_POST) || !wp_verify_nonce($nonce, 'my-nonce') ) wp_send_json_error(); // sends json_encoded success=false
Uses wp_send_json() to send response.
wp_send_json_error() is located in wp-includes/functions.php