plugin development - Why does WordPress add 0 (zero) to an Ajax response?

Unless I end Ajax processing function by exit() or die(), the Ajax function receives desired and correct output but foll

Unless I end Ajax processing function by exit() or die(), the Ajax function receives desired and correct output but following by 0.

Any idea why is that happening?

Is that meant to be like that or it could be fixed?

add_action('wp_ajax_get_homepage', 'get_homepage');
add_action('wp_ajax_nopriv_get_homepage', 'get_homepage');

function get_homepage(){
    echo "get_homepage ->";
    exit();
}

I've read at AJAX in Plugins, but why is that necessary?

Unless I end Ajax processing function by exit() or die(), the Ajax function receives desired and correct output but following by 0.

Any idea why is that happening?

Is that meant to be like that or it could be fixed?

add_action('wp_ajax_get_homepage', 'get_homepage');
add_action('wp_ajax_nopriv_get_homepage', 'get_homepage');

function get_homepage(){
    echo "get_homepage ->";
    exit();
}

I've read at AJAX in Plugins, but why is that necessary?

Share Improve this question edited Oct 6, 2013 at 2:12 Radek asked Oct 4, 2013 at 11:58 RadekRadek 5951 gold badge11 silver badges26 bronze badges 2
  • Does it (the addition of 0) happen even if you're logged out of wordpress? – murdaugh Commented Oct 4, 2013 at 12:06
  • Yes, even if I am not logged into WordPress. – Radek Commented Oct 4, 2013 at 12:14
Add a comment  | 

1 Answer 1

Reset to default 40

The default response from admin-ajax.php is,

die( '0' );

...by adding your own wp_die() or exit() or die() after returning your desired content prevents the default response from admin-ajax.php being returned as well.

It also generally means that your ajax call has succeeded.

Ultimately, to answer your question, it's meant to work this way. What you are doing by exiting after returning your content is the right thing to do.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745231623a4617702.html

相关推荐

  • plugin development - Why does WordPress add 0 (zero) to an Ajax response?

    Unless I end Ajax processing function by exit() or die(), the Ajax function receives desired and correct output but foll

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信