1. #1
    Sencha User
    Join Date
    Sep 2011
    Location
    Taiwan
    Posts
    106
    Vote Rating
    2
    Answers
    1
    cwtuan is on a distinguished road

      0  

    Default Unanswered: How to get server response after call model.save()

    Unanswered: How to get server response after call model.save()


    Hi
    I want to let user know why the request is failed after calling model.save().
    So I let server responses {"success":true,"msg": "XXX"}.
    But it seems that failure function is not able to get response.


    PHP Code:
    record.save({
        
    scopethis,
        
    success: function(recordoperation) {
            
    console.log(operation.response); // I can get server response in success        

        
    },
        
    failure: function(recordoperation) {
            
    console.log(operation.response); // undefined
        
    }
    }); 

    I have seen someone listen on a exception event in proxy to get the response,
    but it's not very graceful. Why does the failure can't get response?

  2. #2
    Sencha - Support Team slemmon's Avatar
    Join Date
    Mar 2009
    Location
    Boise, ID
    Posts
    5,955
    Vote Rating
    212
    Answers
    476
    slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of

      0  

    Default


    You can use a callback config in save() like below:

    Code:
    Ext.define('MyModel', {
        extend: 'Ext.data.Model',
        fields: ['name'],
        proxy: {
            type: 'ajax',
            url: 'data/json.json',
            reader: 'json'
        }
    });
    
    
    var model = Ext.create('MyModel');
    
    
    model.save({
        callback: function (records, operation, success) {
            if (!success) {
                console.log(operation.request.proxy.getReader().rawData.msg);
            }
        }
    });
    Example response:
    Code:
    {
        "success": false,
        "msg": "server response"
    }
    Or if you want to handle every server response globally you can do something like:
    Code:
    Ext.Ajax.on('requestcomplete', function (conn, response, options) {
        var resp = Ext.decode(response.responseText);
        if (resp.success === true) {
            cosnole.log('success = true');
        } else {
            console.log(resp.msg);
        }
    });
    Are you a Sencha products veteran who has wondered what it might be like to work at Sencha? If so, please reach out to our recruiting manager: sheryl@sencha.com

Thread Participants: 1

Tags for this Thread