Expert wanna be
Expert wanna be

Reputation: 10624

Extjs4.1, Retrieve response message after store sync

How to retrieve response message in failure function?

store.sync({
 success : function(){},
 failure : function(response, options){
   console.log(response.responseText); //it does not work, because there is responseText attr in response
 }  
});

Response Text is like this,

{"success":false,"message":"Test Error"}

Anybody know, please advice me.

Thanks

[EDIT]

console.log(response); 

then,

enter image description here

Upvotes: 5

Views: 8371

Answers (3)

chemoish
chemoish

Reputation: 1228

The long and short of it all of these answers are incorrect or inefficient.

  1. Ext.Ajax.on can be used, but you will have to worry about race conditions for requests. Do not use this solution because it can trap you easily. The Ext.Ajax.on might get fired for something other than the sync. See exhibit a.
  2. batch.operations[0].response.responseText can be used too, but this is not reliable way of obtaining the response as the "response" object will not always be populated (It depends on the request and if there are exceptions, 404, 500, success: false, etc.)

Exhibit a

    // This picked up my autocomplete comboboxes load - not what I wanted!
    Ext.Ajax.on({
        requestcomplete: {
            fn: callback,
            scope: this,
            single: true
        },
        requestexecption: {
            fn: callback,
            scope: this,
            single: true
        }
    });

Current solution

This still does not have the response I am looking for, but meh.

    store.sync({
        failure: function (batch, eOpts) {
            // 'this' is the Ext.data.proxy.Ajax object
            // or whatever proxy you are using
            var data = this.getReader().jsonData,
                raw_data = this.getReader().rawData;
        }
    });

I am not sure how this handles my full exception stack of cases, but I will amend my post based on the server-side exceptions I discover (404, 500, etc.)

Upvotes: 2

Peter Kellner
Peter Kellner

Reputation: 15478

I'm not sure if you ever figured this out, but the suggestions above I'm pretty sure are wrong. You need to look at the request exception of the store proxy.

Here is some code to call before you do the store sync.

Ext.Ajax.on('requestexception', function (conn, response, options) {

  if (response.status != 200) {
    var errorData = Ext.JSON.decode(response.responseText);
    Ext.Msg.alert('Creating User Failed',errorData.message);
  }

});

Sorry for digging this old post up but it just hurt to see the answers above since I just went through the same struggle.

HTH's.

Upvotes: 6

Slayer Birden
Slayer Birden

Reputation: 3694

Here's what you need:

store.sync({
    success: function(batch) {
        Ext.Msg.alert('Success!', 'Changes saved successfully.');
    },
    failure: function(batch) {
        Ext.Msg.alert("Failed", batch.operations[0].request.scope.reader.jsonData["message"]);
    }
});

Upvotes: 2

Related Questions