PDA

View Full Version : How to retrive Json error response



rakesh.h
25 Jun 2014, 3:49 AM
Hi,

I have a store with records. I ll do CRUD operations using service calls. During create / update / delete i am using

store.sync(
{
success: function()
{
Ext.Msg.alert('Status', 'Successfully saved');
},
failure: function()
{
Ext.Msg.alert('Attention', 'Failed to save');
},
callback: function()
{
store.load();
},
scope: this
})

If there is a database exception it will be thrown through services.
My question is how can i retrieve exception message from Json response.

Please help me.

Thanks and regards
Rakesh

SebastianZ
25 Jun 2014, 4:09 AM
store.sync({
success: function() {
Ext.Msg.alert('Status', 'Successfully saved');
},
failure: function(result, request) {
var jsonData = Ext.decode(result.responseText);
Ext.Msg.alert('Error', jsonData.msg);
},
callback: function() {
store.load();
},
scope: this
})


As you can see above, the failure (and also the success) function have some params.
With this params you get the JSON-response and decode it.

Hope that helps,

Best regards,
Sebastian

rakesh.h
25 Jun 2014, 5:47 AM
Hi Sebastian,

Thanks for the reply. I am getting 'undefined' at

result.responseText
Is there any other way to get response text. I am using sencha architect 3.0, ext js 4.2.2.

Thanks & regards
Rakesh

SebastianZ
25 Jun 2014, 6:32 AM
oh, my fault, i thought the failure-call works here in a similar way to Ext.Ajax.request.

You can use it this way :


failure: function(batch, options)
{
console.log(batch);
//Do your errorhandling here like:
Ext.Msg.alert("Failed", batch.operations[0].request.scope.reader.jsonData["message"]);
}


Sources:
http://www.sencha.com/forum/showthread.php?282821
http://stackoverflow.com/questions/11884086/extjs4-1-retrieve-response-message-after-store-sync

rakesh.h
30 Jun 2014, 3:17 AM
Hi

I got the solution. We have to use "Ext.data.reader.Json" which we will be added to "Ext.data.proxy.Rest". In the "Ext.data.reader.Json" there is an event called "Exception", from which we can get response text (response.responseText).



onRestException: function(proxy, response, operation, eOpts) {
if(response.responseText.indexOf("Duplicate entry") !== -1)
{
Ext.Msg.alert('Attention', 'Record already exist');
}
else if(operation.action === "create" || operation.action === "update")
{
Ext.Msg.alert('Attention', 'Failed to save');
}
}


It works fine in Ext js 4.2 but i don't know about other versions.

Thanks & regards
Rakesh