我的 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'
}]
});
});
这是小服务程序:
@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);
}
combo.jsp 是包含两个组合框的文件。运行此代码,我收到带有“失败”消息的警报。
它的说明:这一行中的语法错误:
var respText = eval('('+ obj1.responseText +')');
请让我知道如何解决此问题。