Ajax : Why success displays 0?

2019-02-08 08:10发布

I use ajax with jquery and when I tried to display the result in popup. Then alert always displays 0

success : function(results) { 
alert(results); }

EDIT :

 var dataToSend = {
    action:"ACTION_NAME",
    id : "12"
  };
  url_action = 'http://www.______.com/wp-admin/admin-ajax.php';
  $.ajax({
          url: url_action,
          type:'POST',
          data: dataToSend,
          success:function(results)
          {
             alert(results);
          }
   });

4条回答
看我几分像从前
2楼-- · 2019-02-08 08:54

Found it.

It need to add die(); before the end of my own ajax function in function.php.

Because there is one line of script in admin-ajax.php after my own ajax_action that says: die('0'); So we need to die() script before die('0').

查看更多
虎瘦雄心在
3楼-- · 2019-02-08 08:58

May be you are trying without login. so you need to use this action.

add_action('wp_ajax_nopriv_my_action', 'my_action_callback');

you will get response. :)

查看更多
Bombasti
4楼-- · 2019-02-08 09:06

Try to add die(); or exit(); at the very last line of the function.

查看更多
时光不老,我们不散
5楼-- · 2019-02-08 09:10

I had a similar issue with admin-ajax.php returning 0, even i had die().

My problem was that the add_action was declared in a file not loaded default by plugin.

Anyway, if you have problem also check in browser what __.com/wp-admin/admin-ajax.php?action=youraction returns.

查看更多
登录 后发表回答