1

我的代码有问题。
我使用 ExtJs 和 Codeigniter 开发我的 web 应用程序
实际上我的问题是将参数从 extjs 发送到 CI 控制器。
我有一个像这样的简单代码

Ext.Loader.setConfig({enabled: true});
Ext.Loader.setPath('Ext.ux', '../../extjs/src/ux');
Ext.require([
    'Ext.grid.*',
    'Ext.data.*',
    'Ext.ux.grid.FiltersFeature',
    'Ext.toolbar.Paging'
]);

Ext.define('materialspec', {
    extend: 'Ext.data.Model',
    fields: [
             {name: 'id_planmaterial', type: 'string'},
             {name: 'material_desc', type: 'string'},
             {name: 'material_priority', type: 'string'},
             {name: 'material_product_code', type: 'string'},
             {name: 'material_qty', type: 'string'},
             {name: 'material_unit', type: 'string'},
             {name: 'material_note', type: 'string'},
             {name: 'docNo', type: 'string'}
            ]
});

var store2 = Ext.create('Ext.data.Store', {
    id: 'store2',
    model: 'materialspec',
    proxy: {
        type: 'ajax',
        url : '../planspec/planmaterial',
        reader: {
            type: 'json',
            root: 'id_planmaterial'
        }
    },
    sorters: [{
        property    : 'id_planmaterial',
        direction   : 'ASC'
    }]
});

Ext.onReady(function(){
    Ext.QuickTips.init();
    var encode = false;
    var local = true;

    var filters2 = {
        ftype: 'filters',
        encode: encode,
        local: local,
        filters: [
            {
                type: 'boolean',
                dataIndex: 'id_planmaterial'
            }
        ]
    };

    var grid2 = Ext.create('Ext.grid.Panel', {
        border: false,
        store: store2,
        columns: [
            { header: "No.", xtype: 'rownumberer', width: 30, align: 'center' },
            { id: 'docNo', header: "Document No.", dataIndex: 'docNo', sortable: true, filter: {type: 'string'} },
            { id: 'descMaterial', header: "Description", dataIndex: 'material_desc', flex:1, filter: {type: 'string'} },
            { id: 'priority', header: "Priority", dataIndex: 'material_priority', flex:1, filter: {type: 'string'} },
            { id: 'productCode', header: "Product Code", dataIndex: 'material_product_code', flex:1, filter: {type: 'string'} },
            { id: 'qty', header: "Quantity", dataIndex: 'material_qty', flex:1, filter: {type: 'string'} },
            { id: 'unit', header: "Unit", dataIndex: 'material_unit', flex:1, filter: {type: 'string'} },
            { id: 'note', header: "Note", dataIndex: 'material_note', flex:1, filter: {type: 'string'} }
            ],
        loadMask: true,
        features: [filters2],
        title: 'PlanSpec Material',
        height: '100%',
        width: '100%',
        renderTo: 'gridMaterial'
    });

    store2.load({
    params:{
        test:2
    }
    });
});

我有一个这样的控制器

public function planmaterial(){
        $result = $this->input->post('test');
        echo $result;
    }

实际上我认为应该给它一个输出='2'。
但没有显示输出。
所以我该怎么做?
请告诉我我的错误。
感谢您的关注 :)

4

2 回答 2

2

您找错地方了,看起来好像您的路线必须处理直接和通过 XHR 调用的问题。

首先,确保 CI 配置为不破坏$_GET超级全局。它在application/config/config.php

$config['allow_get_array']      = TRUE;

其次,在您的控制器中,使用输入类来确定请求是否实际上是 XHR。您需要知道,因为在一种用途中,您发送了一个变量,而在另一种用途中,您正在发送 JSON 响应。

if ($this->input->is_ajax_request()) {

这仅确定是否HTTP_X_REQUESTED_WITH设置了标头。

最后,您想从$_GET, not获取值$_POST并对其进行初始化:

    $result = $this->input->get('test', FALSE);

你最终得到以下结果:

$value = $this->input->get('test', FALSE);
if ($this->input->is_ajax_request() === TRUE) {
    if ($value === FALSE) {
        /* deal with an improper XHR */
    } else {
        /* deal with a proper XHR, send your JSON */
    }
} else {
    /* Not an AJAX request
     * Adjust your view based on the value of 'test' being set or not. Do you
     * need to do something differently if it isn't set? 
     */
}

但是,我建议您使用不同的路线,其中一种专门用于 XHR - 至少据我看您发布的内容。

于 2012-12-10T03:28:36.890 回答
0

这是执行 GET 请求并使用查询字符串:

../planspec/planmaterial?test=2

然后,您应该使用:

$this->input->get('test', TRUE);

祝你好运!

于 2012-12-10T03:05:31.867 回答