Why does AJAX json script return extra 0 (zero)

 ̄綄美尐妖づ 提交于 2019-12-05 12:09:02

Your function should use wp_send_json to encode the PHP as JSON and sent it back to the AJAX request handler. This will also stop executing of any following PHP too, so there is no need to use exit or die.

So for your specific example, you would use:

/**
* Get Facebook Likes
*/

public function get_facebook_likes(){
   wp_send_json(99);
}

This is old question but I'm going to answer this. wp_send_json() function may help but not always. There could be some moments when you can't use this function. Like, when you load posts by ajax, you are getting some template part, you can use the function. That's why WordPress documentation suggests to make use of the die() function.

So in the end, your php function should look like this:

/**
* Get Facebook Likes
*/

public function get_facebook_likes() {
   echo 99;
   die();
}

Use Firebug and view the actual net data transmitted and received. Determine if the error is coming from the javascript side or the PHP side. Copy the net request and paste it into a separate browser window to see the raw result. If it is PHP, pursue that. if it is the javascript doing something, let us know.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!