I have a json which is return from the java code. I use toJSON to show the json but it does’nt showing anything and giving an error when i de-bugged it using firebug.Below is the response which i have to show in browser.
{"status":"0","Response":{
"name":"abc";
"gender":"male";
}
}
below is the function in jsp which i am calling:
$.ajax({
type: 'post',
url:$("#abc").val(),
dataType:'json',
success:function(data) {
alert(data);
var json = $.toJSON(data);
alert(json);
$("#response").val(json);
},
error:function() {
alert("request failed");
}
});
1st alert shows [object] but 2nd alert is not showing anything.
There is no such thing as
$.toJSON(), thats why you get an error. UseJSON.stringify()andJSON.parse().Alternatively, if you set the correct contentType, you should be able to use it without any conversions.
edit: to be correct
toJSON()is a plugin. Imo there is no need for this, the standard JSON-handling from the browsers and jQuery is sufficient for that task.