How To Make AJAX Calls With ExtJS Easily

By Angsuman Chakraborty, Gaea News Network
Tuesday, August 14, 2007

Simple things should be simple. While starting with ExtJS, I saw a screencast on Grid which used a rather dubious method of making an AJAX call using an actual form element. I hunted for a better option and I came across a better option - Ext.data.Connection. Here’s an working example on how you can easily make AJAX call in ExtJS library.


var conn = new Ext.data.Connection();
conn.request({
    url: 'history.jsp',
    method: 'POST',
    params: {"metaID": metaID, columnName: field},
    success: function(responseObject) {
        showHistoryDialog(responseObject.responseText);
    },
     failure: function() {
         Ext.Msg.alert('Status', 'Unable to show history at this time. Please try again later.');
     }
});

Obviously you will have to implement the showHistoryDialog() method to your taste. Change the method names and url to suit your requirements.

The downside is that it doesn’t display a loading message which you can easily implement.

Update:
Here is the full code showing a Loading dialog too:


var conn = new Ext.data.Connection();

    // History buton click handler. It submits the request and displays the response using history dialog
    function showHistory() {
        if(record != null && field != null) {
            metaID = record.get("MetaID");
            grid.getGridEl().mask('Loading history...');
            conn.request({
                url: 'history.jsp',
                method: 'POST',
                params: {"metaID": metaID, columnName: field},
                success: function(responseObject) {
                    showHistoryDialog(responseObject.responseText);
                    grid.getGridEl().unmask(true);
                },
                failure: function() {
                    grid.getGridEl().unmask(true);
                    Ext.Msg.alert('Status', 'Unable to show history at this time. Please try again later.');
                }
            });
        }
    }

Note: The code is used in a production environment to display historical information. The server side code as well as the implementation of of showHistoryDialog() is not provided as it is irrelevant to the context.

With libraries like ExtJS and services like GMail, browser is now truly the king. You don’t need desktop applications for most purposes.

Discussion

mikail
July 13, 2010: 2:04 am

thank you for example, i want to make a login page with sencha touch and i wonder is it possible to work with sencha touch and jsp? i want to send username and password to jsp file for checking i but i dont know how to make it ? can you help me pls, thanks in advance


Raghavendra
June 2, 2009: 12:33 pm

Hi,

Thank you for this code.Thank you very much!

September 29, 2008: 11:40 am

Excellent post! I totally digg it ;-).


spectrus
March 11, 2008: 4:09 pm

Thank you very much! After much digging around (including extjs.com), that\\\’s exactly what I need.


arnold
August 27, 2007: 2:13 am

Ext kan eenvoudige zaken nog eenvoudiger maken.
Je kan Ext.Ajax.request gebruiken om een
call uit te voeren, is nog net iets korter dan de connection oplossing.
Overigens gebruikt ook eens Ext.encode om
je parms te encoderen,

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :