Wednesday, 15 September 2010

php - Parsing JSON Objects in JavaScript -



php - Parsing JSON Objects in JavaScript -

i have php on server handling ajax phone call , returning json object so:

$dataarray = array('order_id'=>$order_id, 'response'=>'sucessfully added'); header('content-type: application/json'); echo json_encode( $dataarray );

this ajax call:

$('.add').ajaxform({url: this.href, type:'post', data: this.serialize, datatype: 'json', success: function(responsetext){ var p = json.parse(responsetext); alert(p.response); $('.popupcontainer').hide(); } });

in firebug can see reaches line begins 'var p' fine. in fact, @ point, fb tells me responsetext want be: {"order_id":"182","response":"sucessfully added"}. @ point stops, must missing here.

there no need manual parsing, since datatype set json response automatically parsed

$('.add').ajaxform({ url: this.href, type: 'post', data: this.serialize, datatype: 'json', success: function (p) { alert(p.response); $('.popupcontainer').hide(); } });

jquery form

datatype

'json': if datatype == 'json' server response evaluted , passed 'success' callback, if specified

success

responsetext or responsexml value (depending on value of datatype option).

javascript php jquery ajax json

No comments:

Post a Comment