ajax die,javascript - Why does Wordpress ajax die('0') - Stack Overflow

I added the following to my functions.php file:

add_action('wp_ajax_send_email', 'send_email_callback');

add_action('wp_ajax_nopriv_send_email', 'send_email_callback');

So i added the following callback function:

send_email_callback()

{

//do some processing

echo json_encode(array('response'=>'Ya man it worked.'));

//return control back to *-ajax.php

}

This is what is returned to my javascript :

{"response":"Ya man it worked."}0

So ofcourse when it reaches the $.parseJSON( msg ) line I get Uncaught SyntaxError: Unexpected number.

var request = $.ajax({

url: "/wp-admin/admin-ajax.php",

method: "POST",

data: { action : 'send_email', P : container },

dataType: "html"

});

request.done(function( msg ) {

var obj = $.parseJSON( msg );

console.log(obj.response);

});

request.fail(function( jqXHR, textStatus ) {

alert( "Request failed: " + textStatus );

});

});

So where does this 0 come from? admin-ajax.php

it says on line 97:

// Default status

die( '0' );

Why is this here, why am I reaching the die('0') line? Shouldnt it be just die() so it doesnt mess up my response?

Seems the only way to fix this is either modify admin-ajax.php or simply die() at the end of my send_email_callback() function.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值