samedi 18 avril 2015

php json_encode() adds an extra character zero at the end [on hold]

I'm trying to parse a simple php associative array to javascript using json_encode, but for some unknown reason, the end result has an extra zero (0) on the end. I've looked around but can't find an answer anywhere, does anyone know what might be happening? This is a call-back from an AJAX call. I simplified the array to ensure it wasn't my array structure and it still persists.



$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);


Produces



"{"a":1,"b":2,"c":3,"d":4,"e":5}0"


note the extra 0 on the end, which is causing jQuery.parseJSON to throw an error. I'm using Google Chrome for javascript debugging, and PHP with NetBeans, Wamp, in Wordpress.


SOLVED: I needed to die(); in the php callback function. Not doing this causes Wordpress to die('0'); hence the extra zero. Apologies for not including the callback function, that probably would have helped. Thanks all the same.


Aucun commentaire:

Enregistrer un commentaire