1. #1
    Sencha User
    Join Date
    Mar 2012
    Posts
    56
    Vote Rating
    0
    aorlic is on a distinguished road

      0  

    Default App specific data when success == false

    App specific data when success == false


    I'm using Json-Ajax to get data from the server with reader's "success" property to pick-up the execution state.

    I set the "success" prop. on the server side to indicate some exception occurred there. Along with "success==false", I send some more info about the error, so this can be displayed to the end-user.

    However, when the "success==false", I don't know how to get this additional info. Above is the example (I need to get to "error" part):

    Code:
    {
        "success": false,
        "error": {
            "id": 71,
            "code": 3,
            "message": "Access denied",
            "action": "Please report this to your admin"
        }
    }

  2. #2
    Sencha Premium Member
    Join Date
    Oct 2009
    Location
    Leeds
    Posts
    64
    Vote Rating
    9
    damo will become famous soon enough

      0  

    Default


    In the success callback for the ajax request decode the json and check what the sucesss property equals, then show the data as required.

    E.g. Extjs3 code
    Code:
    Ext.Ajax.request({
    url: 'text.json', success: function(response, opts){
    var obj = Ext.decode(response.responseText); if(obj.success !== true){
    console.log(obj.error.message);
    }
    }
    });

  3. #3
    Sencha User
    Join Date
    Mar 2012
    Posts
    56
    Vote Rating
    0
    aorlic is on a distinguished road

      0  

    Default


    Thanks!

    I'm using Ext JS 4 and send request indirectly through the Store object (e.g. load()). Here I provide the callback funtion which later receives the following parameters:
    - success
    - records (always undefined if success == false)
    - operation (??)

    I am not sure how to extract the rest of the response JSon if the success == false.

    Your answer does not help a lot, as the parameters are completely different.
    Thanks once more and regards!

  4. #4
    Sencha Premium Member
    Join Date
    Oct 2009
    Location
    Leeds
    Posts
    64
    Vote Rating
    9
    damo will become famous soon enough

      0  

    Default


    I was going to say to extend Ext.data.proxy.Ajax but I don't think it's needed.

    http://docs.sencha.com/ext-js/4-0/so...ata-proxy-Ajax
    http://docs.sencha.com/ext-js/4-0/so...a-proxy-Server

    When success is false the processResponse method on the Ajax proxy does this.


    operation.setException(result.message);me.fireEvent('exception', this, response, operation);Therefore is you change the returned ajax to instead having error as the object and have message as the object containing all the error data you can then use the hasException() and getError() of the Ext.data.Operation.

  5. #5
    Sencha User
    Join Date
    Mar 2012
    Posts
    56
    Vote Rating
    0
    aorlic is on a distinguished road

      0  

    Default


    Thank you once more damo.

    Sorry, but I really don't understand your answer.

  6. #6
    Sencha Premium Member
    Join Date
    Oct 2009
    Location
    Leeds
    Posts
    64
    Vote Rating
    9
    damo will become famous soon enough

      0  

    Default


    Ok no worries.

    Firstly change the json response to like so:

    PHP Code:
    {
        
    "success"false
       
    "message": {
            
    "id"71
           
    "code"3,
            
    "message""Access denied",
            
    "action""Please report this to your admin"
        
    }
     } 
    Now as part of the callback using the getError() method of operation it will return the object containing your error data.
    PHP Code:
    .load({
     
    callback: function(recordsoperationsuccess){
      if(
    operation.hasException()){
       var 
    errorObj operation.getError();
      }
     }
    }) 

  7. #7
    Sencha User
    Join Date
    Mar 2012
    Posts
    56
    Vote Rating
    0
    aorlic is on a distinguished road

      0  

    Default


    That's it, thanks!!

Thread Participants: 1