使用 json 对象重定向



我的UI中有两个组合框。第一个组合框包含国家/地区名称列表,第二个组合框包含该国家/地区中存在的州列表。从第一个组合开始,当我选择国家/地区时,名称将发送到 servlet。使用该名称,访问数据库,检索状态名称列表并将其转换为JSONObject。现在我无法将此 JSONObject 传递回 extjs 文件,以便使用状态列表填充第二个组合框。

以下是 js 文件的代码:

    Ext.require('Ext.tab.*');
Ext.require('Ext.button.*');
Ext.define('Country',{
    extend: 'Ext.data.Model',
    fields: [
        { name: 'id', type: 'string' },
        { name: 'name', type: 'string' }
    ]
});
Ext.define('CountryCombo', {
    extend: 'Ext.form.field.ComboBox',
    alias: 'widget.countrycombo',
    allowBlank: false,
    queryMode: 'local',
    valueField: 'id',
    displayField: 'name',
    store: {
        model: 'Country',
        data: [
            { id: 'China', name: 'China'},
            { id: 'Japan', name: 'Japan'},
            { id: 'Malaysia', name: 'Malaysia'}
        ]
    },
    listeners: {
        "select": function(obj){  
            Ext.Ajax.request({
                url: '/CellEditing/FormServlet',
                method: 'POST',
                params: {
                    data: obj.getValue()
                },
                success: function(obj){
                    alert('sucess');
                    var respText = eval('('+ obj.responseText +')');
                    alert(respText);
                },
                failure: function(obj){
                    alert('failure');
                }
            });                 
        }
    }
});
Ext.define('State',{
    extend: 'Ext.data.Model',
    fields: [
        { name: 'id', type: 'int' },
        { name: 'name', type: 'string' }
    ]
});
Ext.define('StateCombo', {
    extend: 'Ext.form.field.ComboBox',
    alias: 'widget.statecombo',
    allowBlank: false,
    queryMode: 'local',
    valueField: 'id',
    displayField: 'name',
    store: {
        model: 'State',
        data:[]
    }
});
Ext.onReady(function(){
    Ext.tip.QuickTipManager.init();
    Ext.widget('panel', {
        renderTo: 'pan1',
        title: 'Basic Panel',
        width:600,
        height:100,
        defaults: {
            bodyPadding: 10,
            border: false,
            xtype: 'panel',
            layout: 'anchor'
        },
        layout: 'hbox',
        items: [{
                  fieldLabel: 'Country',
                  xtype: 'countrycombo',
                  width: 234,
                  margin: '5 5 5 5'
               },{
                  fieldLabel: 'State',
                  xtype: 'statecombo',
                  width: 234,
                  margin: '5 5 5 5'
               }]            
    });  
});

下面是 servlet:

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    // TODO Auto-generated method stub
    System.out.println("Inside Post");
    String selectedValue = req.getParameter("data");
    System.out.println(selectedValue);
    //This is the json string I want to send back to the UI
    //The format of this json is correct, I verifieded it printing in console.
    String jsonString = new StateHelper().getStates(selectedValue);
    //Below are the lines I am having the doubt, it is not correct.
    req.setAttribute("data", jsonString);
    req.getRequestDispatcher("combo.jsp").forward(req, resp);
}

组合.jsp是包含两个组合框的文件。运行此代码,我收到带有"失败"消息的警报。

它告诉:这一行中的语法错误:

var respText = eval('('+ obj1.responseText +')');

请让我知道如何解决此问题。

我脑海中浮现出两个想法。

首先为什么你使用POST而不是GET?

第二次尝试使用以下方式设置请求/响应标头:

'Accept' : 'application/json'
'Content-type' : 'application/json'

var respText = Ext.JSON.decode( obj.responseText );

最新更新