javascript - Why does Wordpress ajax die('0') -


i added following functions.php file:

add_action('wp_ajax_send_email', 'send_email_callback'); add_action('wp_ajax_nopriv_send_email', 'send_email_callback'); 

so added following callback function:

send_email_callback() { //do processing  echo json_encode(array('response'=>'ya man worked.')); //return control *-ajax.php } 

this returned javascript :

{"response":"ya man worked."}0 

so ofcourse when reaches $.parsejson( msg ) line uncaught syntaxerror: unexpected number.

  var request = $.ajax({             url: "/wp-admin/admin-ajax.php",             method: "post",             data: { action : 'send_email', p : container },             datatype: "html"         });          request.done(function( msg ) {             var obj = $.parsejson( msg );             console.log(obj.response);          });          request.fail(function( jqxhr, textstatus ) {             alert( "request failed: " + textstatus );         });     }); 

so 0 come from? admin-ajax.php says on line 97:

// default status die( '0' ); 

why here, why reaching die('0') line? shouldnt die() doesnt mess response?

seems way fix either modify admin-ajax.php or die() @ end of send_email_callback() function.

in wordpress ajax actions, aren't supposed return response, echo response , call die yourself. wordpress continue call of ajax registered callbacks until 1 of callbacks kills execution, final 1 dieing response of 0.

example wordpress codec ajax in plugins

<?php   add_action( 'wp_ajax_my_action', 'my_action_callback' );  function my_action_callback() {     global $wpdb; // how access database      $whatever = intval( $_post['whatever'] );      $whatever += 10;          echo $whatever;      wp_die(); // required terminate , return proper response } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -