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

How to make an ajax call from a function of controller in EXTJS

本文关键字:函数 ajax 调用 控制器 EXTJS      更新时间:2023-09-26

我有一个控制器在我的应用程序,从那里我必须通过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是webservice url, barData是要传递给服务器的数据。该url必须与应用程序url相同。Same_origin_policy