javascript - json_encode putting empty array on the end of json -
i'm writing ajax application , have function in php script:
public function expire_user() { $r=array("return"=>'ok'); echo json_encode($r); }
that gets called javascript:
$.getjson("/users/expire_user",function(data){ alert('success'); });
the problem alert never displays. think due json_encode returning invalid json, because when go url directly, displays
{"return":"ok"}[]
which not valid json due '[]' on end. why json_encode putting empty array on end , how rid of can receive valid json?
wild guess, maybe should set correct headers json in php function this:
public function expire_user() { $r=array("return"=>'ok'); header("content-type: application/json"); echo json_encode($r); }
or send content x-json
headers this:
public function expire_user() { $r=array("return"=>'ok'); $json_data = json_encode($r); header('x-json: (' . $json_data . ')'); header('content-type: application/x-json'); echo $json_data; }
a bit rusty on whether when using x-json
accompanying header should application/x-json
or normal application/json
, adding caveat debug.
Comments
Post a Comment