Yogendra Singh
Yogendra Singh

Reputation: 553

how to get the server response.responseText after store load extjs 4

I'm having one problem with getting the response.responseText from the server response in extjs 4.

Below is my code to load the store:

store.load({
    params: {
        'projectid': this.projectid
    },
    callback: function (records, operation, success, response) {
        console.log(records);
        console.log(response.responseText);
    }
});

Actually, when I made the request with the below function, I properly get the reponse.responseText.

Ext.Ajax.request({
    url: 'login/GetLoginCheck.action',
    method: 'GET',
    params: {
        'username': values['username'],
        'password': values['password']
        },
    scope: this,
    success: function(response) {
        Ext.Msg.alert(response.responseText);
        var redirect = response.responseText;
        window.location.href = "" + redirect + ".jsp";
    },
    failure: function(response) {
        Ext.Msg.alert('INVALID USERNAME OR PASSWORD');
    }
});

So please suggest me how can I get the response.responseText from the store.load() having a callback function.

Upvotes: 7

Views: 21563

Answers (6)

krishna
krishna

Reputation: 29

In Extjs 4.x it is working like this

myStore.load({
        url: 'myurl',
        method: 'GET',
        callback: function(records, operation, success) {
        var jsonStr = Ext.JSON.decode(operation.response.responseText);
            alert(jsonStr.message);
         }
});

In Extjs 5 you have to do like this

myStore.load({
        url: 'myurl',
        method: 'GET',
        callback: function(records, operation, success) {
 var message=forecastMethodStore.getProxy().getReader().rawData.message;
         }
});

But the key point here is you should set the message in JSON response from java side.

Sample: {"Root":[], "message":"duplicates"}"

Hope this will help someone.

Upvotes: 0

Mr. Mak
Mr. Mak

Reputation: 867

You must set messageProperty in proxy reader in your 'Ext.data.Store'.

            reader: {
                type: 'json',
                root: 'myDataList',
                totalProperty: 'myTotalRecord',
                successProperty: 'mySuccess',
                messageProperty : 'myMsg'
            }

when mySuccess returns false then invoked callback: function.

    store.load({
        params: {start: 0, limit: 15},
        callback: function (records, operation, success) {
            if (!success) {
                try {
                    Ext.Msg.alert('Sorry !', operation.getError());
                    // operation.getError() returns myMsg value
                }catch (e){
                    Ext.Msg.alert('Exception !', e);
                }
            }
        }
    });

Here is a json return from Java Servlet.

    Map<String, Object> myDataMap = new HashMap<>(3);
    try {
        // Something
        myDataMap.put("mySuccess", true);
        myDataMap.put("myMsg", "Whats up khomeni !");
    } catch (Exception e) {
        myDataMap.put("mySuccess", false);
        myDataMap.put("myMsg", "Whats wrong with me.");
    }
    String json = new Gson().toJson(myDataMap);

Upvotes: 0

Adamson
Adamson

Reputation: 11

You may also try this..

Ext.create('Ext.data.Store',{
    fields[],
    proxy:{url:'store_url.json', reader:{type:'json',root:'data'}},
    autoLoad:true,
    listeners:{
        load:function(store, record, success, opts){
            var response_text = store.proxy.reader.rawData;
            console.log(response_text);
        }
    }
})

Upvotes: 1

kuldarim
kuldarim

Reputation: 1106

In extjs 3.4 you can use this:

this.historyInvoiceHeaderGrid.store.load({
        params:{start:0, limit:20},
        callback: function (records, operation, success) {
            console.log(this.reader.jsonData);
        }});

This property store.reader.jsonData will return full response.

Maybe for someone it would be usefull in extjs 3.

Upvotes: 0

user1636522
user1636522

Reputation:

I have faced a similar problem using Model.load(...), but in my case, operation.response was not defined. So, I have found another way to get it :

Model.load(1, {
    success: function () {
        // I haven't tested inside this callback yet
    },
    failure: function (record, operation) {
        var response = operation.request.proxy.reader.rawData;
        alert(response.message);
    }
});

Upvotes: 1

scebotari66
scebotari66

Reputation: 3480

callback has 3 parameters... try this :

store.load({
    params: {
        'projectid': this.projectid
    },
    callback: function (records, operation, success) {
        console.log(operation.response.responseText);
    }
});

Upvotes: 8

Related Questions