.post()请求的数字响应是添加0

我正在使用Wordpress ajax api将.pht函数的结果发送到客户端.post()ajax。 问题是返回0的值以及数值。 因此,如果数值为“3”,我得到“30”。

所以这里我有一个php函数,我用它来获取user_id并返回该用户相应的看不见的注释计数。

add_action("wp_ajax_return_unseen_comment_count","return_unseen_comment_count"); function return_unseen_comment_count() { $user_id = $_POST['userId']; $count = get_unseen_comment_count($user_id); echo $count; } 

如果我在$ count之后放置“test”这个词,我会得到“3test0”。

然后,我使用间隔来检查新看不见的注释的function。

 $(document).ready(function() { var userId = fodAjax.user_id; var cmntCount = $(".cmnt-cnt").text(); if (userId > 0) { setInterval(function() { data = { action: 'return_unseen_comment_count', userId: userId }; $.post(ajaxurl, data, function (response) { console.log(response); }); }, 10000); //10 seconds } }); 

现在我在另一个函数中使用get_unseen_comment_count($user_id)来显示我的模板中的计数,它返回正确的值“3”,所以它没有做任何额外的应该添加0.我在这里缺少什么?

你必须写exit;echo语句之后。

如果请求url是wp-admin / admin-ajax.php时AJAX请求失败,它将返回-1或0,具体取决于失败的原因。 此外,如果AJAX请求成功,它将返回0

参考 。