Javascript 如何从EXTJS中的控制器函数进行ajax调用

Javascript 如何从EXTJS中的控制器函数进行ajax调用,javascript,sencha-touch,extjs4.1,Javascript,Sencha Touch,Extjs4.1,我的应用程序中有一个控制器,从这里我必须通过Ajax调用访问Servlet。控制器函数的Ajax调用的正确语法是什么。下面是我的代码 Ext.define('Gamma.controller.ControlFile', { extend : 'Ext.app.Controller', //define the stores stores : ['BarColumn','RadarView','VoiceCallStore','SMSCallStore','MMSCallStore','GPR

我的应用程序中有一个控制器,从这里我必须通过Ajax调用访问Servlet。控制器函数的Ajax调用的正确语法是什么。下面是我的代码

Ext.define('Gamma.controller.ControlFile', {

extend : 'Ext.app.Controller',

//define the stores
stores : ['BarColumn','RadarView','VoiceCallStore','SMSCallStore','MMSCallStore','GPRSUsageStore'],
//define the models 
models : ['BarCol','radar','VoiceCallModel','SMSCallModel','MMSCallModel','GPRSUsageModel'],
//define the views
views : ['BarColumnChart','LineChart','RadarChart','VoicePie','SMSPie','MMSPie','GPRSPie'],  



initializedEvents: false,
init: function() {
    this.control({
        '#barColumnChart': {
            afterlayout: this.afterChartLayout
        }
    });
},
afterChartLayout: function(){
    var me=this;
    if(this.initializedEvents==true) return;
    this.initializedEvents=true;
    Ext.getCmp('barColumnChart').series.items[0].on('itemmousedown',function(obj){

       // alert(obj.storeItem.data['source']+ ' &' + obj.storeItem.data['count']);

      var barData=obj.storeItem.data['source']+ ' &' + obj.storeItem.data['count'];

      me.dataBaseCall(barData);
    });
},
   dataBaseCall: function(barData){
  // i have to call ajax request here 
   // my Servlet name is TopCount  

这可能与您想要的非常接近:

Ext.define('Gamma.controller.ControlFile', {

    extend : 'Ext.app.Controller',

    //define the stores
    stores : ['BarColumn','RadarView','VoiceCallStore','SMSCallStore','MMSCallStore','GPRSUsageStore'],
    //define the models 
    models : ['BarCol','radar','VoiceCallModel','SMSCallModel','MMSCallModel','GPRSUsageModel'],
    //define the views
    views : ['BarColumnChart','LineChart','RadarChart','VoicePie','SMSPie','MMSPie','GPRSPie'],  



    initializedEvents: false,
    init: function() {
        this.control({
            '#barColumnChart': {
                afterlayout: this.afterChartLayout
            }
        });
    },
    afterChartLayout: function(){
        var me=this;
        if(this.initializedEvents==true) return;
        this.initializedEvents=true;
        Ext.getCmp('barColumnChart').series.items[0].on('itemmousedown',function(obj){

            // alert(obj.storeItem.data['source']+ ' &' + obj.storeItem.data['count']);

            var barData=obj.storeItem.data['source']+ ' &' + obj.storeItem.data['count'];

            me.dataBaseCall(obj.storeItem.data['source'], obj.storeItem.data['count']);
        });
    },
    dataBaseCall: function(source, count){
        // i have to call ajax request here 
        // my Servlet name is TopCount
        Ext.Ajax.request({
            url: "TopCount",
            success: function(response, opts){
                //do what you want with the response here
            },
            failure: function(response, opts) {
                alert("server-side failure with status code " + response.status);
            },
            params: {
                source: source,
                count:  count
            }
        });
    }
});
试试这个

dataBaseCall: function (barData,urlx){
var params = JSON.stringify(barData);
    var xhReq = new XMLHttpRequest();
    xhReq.open("POST", urlx, false);

    xhReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    xhReq.send(params);
    var json_object = JSON.parse(xhReq.responseText);
    if (!json_object) {
        console.log('Server Returned Null');
    }else{
         console.log(json_object);
    }
}

其中urlx是Web服务url,barData是要传递到服务器的数据。url必须与应用程序url位于同一域中

感谢您的回复Reimius。。但它说“未捕获的引用错误:源未定义”。。当使用Extjs框架时,我应该怎么做?使用这种方法是很笨重的。。。而且不是异步的。