why ajax call return an extra 0 at end in WordPress?

If your use the returned data of an ajax call, you will find there is an unexpected extra 0 in the data. For example, if your action function is:
function myfunction()
{
echo "hello world";
}

you will get “hello world0″.

This is because after myfunction is executed, there is some other code executed that outputs the extra 0. Specifically, the last line in admin-ajax.php is die( '0' ); which outputs the extra 0.

To avoid this situation, you should use wp_send_json("hello world") in myfunction() instead of using echo.

If you like my content, please consider buying me a coffee. Buy me a coffeeBuy me a coffee Thank you for your support!

Leave a Reply