如何从EXTJS中的控制器功能进行ajax调用

时间:2013-05-07 13:54:01

标签: 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','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  

2 个答案:

答案 0 :(得分:2)

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

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
            }
        });
    }
});

答案 1 :(得分:0)

试试这个

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是webservice url,barData是要传递给服务器的数据。url必须与应用程序URL Same_origin_policy

的域相同