Reputation: 11
I have a datagrid using the jqGrid plugin that I wanted to have a custom error message if the email entered already exist in the database. It passes the fields to the cfc and returns this json (this is what shows in the Firebug window so that part is ok): {"USERDATA":{"MSG":"Email already exists in registration","TYPE":"Error"}}
So basically I'm trying to parse out this json and make an alert or do something with the modal window. I found the following code here and it seems to pass the json fine. Here's the part of the add options that calls the GetResponseData function:
{addCaption:"Add Recipient",closeOnEscape:true,savekey: [true,13],closeAfterEdit : false, errorTextFormat:commonError,width:"450"
,afterSubmit:function(response,postdata){ return GetResponseData(response); },reloadAfterSubmit:true,bottominfo:"Fields marked with (*) are required",top:"60",left:"70"}
The function with alerts added to test:
function GetResponseData (resp) {
var jtxt=(resp.responseText); //{"USERDATA":{"MSG":"Email already exists in registration","TYPE":"Error"}}
var jreturn=JSON.parse(jtxt)
alert(jreturn); //shows '[Object.object]'
var msg=jreturn.USERDATA[0].MSG;
var type=jreturn.USERDATA[0].TYPE;
alert(msg);
alert(type);
}
I've tried JSON.parse and eval() both with the same error: resp.USERDATA is undefined
Thanks everyone! I'm fairly new to CF and jquery, any help is appreciated.
Upvotes: 1
Views: 337
Reputation: 1258
If you're using Firebug or Google Chrome I recommend using the console view and replacing your alerts for console.log() calls. This will let you inspect the objects and see how they are structured.
Try
console.log(jreturn)
I see that you are using jreturn.USERDATA[0].MSG; . Why are are you accessing the USERDATA as if it were an array? For what we see as the output for the responseText this should be
jreturn.USERDATA.MSG;
Upvotes: 2