php - json_encode and parsing information if there are no records returned -
i make ajax (php + jquery), have 1 problem. have: datatype:"json",
in javascript
everything works fine when json_encode returning records db. problem do, how server in case there not records?
when have array encoded json ok. when array empty json_enocde returning null. not in expected format can't catch error.
hope, question clear :)
here 2 (of several) possibilities:
php option:
handle null object make parseble ahead of time...
if ($mydata==null) { $mydata = new stdclass(); } echo json_decode($mydata);
js option:
try catching error when parsing on client side.
// catch error try { var data = json.parse(json); } catch(error) { // handle error }
Comments
Post a Comment