PDA

View Full Version : decode response from server



norizam61
20 May 2010, 8:42 AM
Hi

I m developing a student database system, How do I decode response from server :

The result from firebug


{success: false, "data":[{"Record Not Found"}]}


I have tried all day using 'action.result.data', using 'Ext.util.JSON.decode(action.responseText).data', using
'action.response.data'. All not succeed to show 'Record Not Found' to user.

:-/

norizam61
20 May 2010, 5:46 PM
any suggestion?

:-/

mankz
20 May 2010, 9:54 PM
The data sent by your server is invalid json. JSON is key-value pairs, your response is just a value, that's why you won't be able to decode it using Ext decode.

CrazyEnigma
21 May 2010, 5:22 AM
As mankz put it, it is not properly formed JSON.

Think about the implications of your return. If you decode it, what does it mean? How can you obtain the value?


var obj = Ext.decode(response.responseText);

How do you access data?


var data = obj.data

data is an object, but this object contains a string but no reference. The reference is "Record not found" So you can access the record by:


data["Record not found"]

but it has no value, nor it has a key.
If it was like this, then you can access the record.


{"success": false, "data":["Record Not Found"]}

with (NOTE: no object syntax)


obj.data[0]; // (data is the object of array with size 1 of string)

But what you really want is an error message key. Like:


{"success": false, "error":"Record Not Found","data":[],"count":0}

norizam61
21 May 2010, 5:39 AM
Hi Enigma

Very good explanation!
Thanks for helping, you solve the problem...