0 post 报红_.post()请求的数值响应添加了0

I'm using the Wordpress ajax api to send the result of a php function to the client with .post() ajax. The problem is the value returning a 0 aswell as the numeric value. So if the numeric value is "3" I get "30".

So here I have a php function that I use to grab the user_id and return the corresponding unseen comment count for that user.

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;

}

If I place the word "test" after $count I'll get "3test0".

I then use an interval to check function for new unseen comments.

$(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

}

});

Now I use get_unseen_comment_count($user_id) in another function to display the count in my template and it returns the correct value "3" so it's not doing anything extra that should be adding the 0. What am I missing here?

解决方案

You must write exit; after echo statement.

If the AJAX request fails when the request url is

wp-admin/admin-ajax.php, it will return either -1 or 0 depending on

the reason it failed. Additionally, if an AJAX request succeeds, it

will return a 0.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值