Results 1 to 5 of 5

Thread: How to retrive Json error response

  1. #1
    Sencha User
    Join Date
    Apr 2014
    Posts
    19
    Answers
    2

    Default Answered: How to retrive Json error response

    Hi,

    I have a store with records. I ll do CRUD operations using service calls. During create / update / delete i am using
    Code:
    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

  2. 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).

    Code:
    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

  3. #2
    Sencha Premium User
    Join Date
    Nov 2012
    Location
    Austria
    Posts
    55
    Answers
    1

    Default

    PHP Code:
    store.sync({
        
    success: function() {
            
    Ext.Msg.alert('Status''Successfully saved');
        },
        
    failure: function(resultrequest) {
            var 
    jsonData Ext.decode(result.responseText);
            
    Ext.Msg.alert('Error'jsonData.msg);
        },
        
    callback: function() {
            
    store.load();
        },
        
    scopethis
    }) 
    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

  4. #3
    Sencha User
    Join Date
    Apr 2014
    Posts
    19
    Answers
    2

    Default

    Hi Sebastian,

    Thanks for the reply. I am getting 'undefined' at
    Code:
    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

  5. #4
    Sencha Premium User
    Join Date
    Nov 2012
    Location
    Austria
    Posts
    55
    Answers
    1

    Default

    oh, my fault, i thought the failure-call works here in a similar way to Ext.Ajax.request.

    You can use it this way :
    PHP Code:
    failure: function(batchoptions)
    {
      
    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/1...ter-store-sync

  6. #5
    Sencha User
    Join Date
    Apr 2014
    Posts
    19
    Answers
    2

    Default

    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).

    Code:
    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •