我使用的是ExtJs 3.4,我有一个组合框。当用户从此组合中选择一个值时,将调用Ajax请求以根据所选值从我的postgresql数据库中检索数据。
listeners : {
select : function() {
var selVehicleCode = Ext.getCmp('vehicleCombo').getValue();
var selFromDate = Ext.getCmp('fromDateCombo').getValue();
Ext.Ajax.request({
method: 'GET',
loadMask: true,
scope: this,
url: "http://" + host + ":" + port + "/" + projectName + "/"
+ "SetDefaultValues",
success: function (response, request) {
Ext.MessageBox.alert('success', response.responseText);
},
failure: function (response, request) {
Ext.MessageBox.alert('failure', response.responseText);
},
params : {
selectedVCode : selVehicleCode,
selectedFromDate : selFromDate.format('Y-m-d')
}
});
}
}
这是我的SetDefaultValues.java servlet。
//imports
public class SetDefaultValues extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SetDefaultValues() {
super();
// TODO Auto-generated constructor stub
}
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
processRequest(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
processRequest(request, response);
}
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
ServletContext context = getServletContext();
String dbName = context.getInitParameter("ConnectionDB");
String connectionHost = context.getInitParameter("ConnectionHost");
String connectionUser = context.getInitParameter("ConnectionUser");
String connectionPassword = context
.getInitParameter("ConnectionPassword");
String port = "5433";
Statement statement = null;
ResultSet defaultSet = null;
Connection pgConnection = null;
// String lineString = "";
try {
pgConnection = ConnectionManager.getPostgresConnection(
connectionHost, connectionUser, connectionPassword, dbName,
port);
// out.println(connectionHost+","+ connectionUser+","+
// connectionPassword+","+ dbName);
statement = pgConnection.createStatement();
// out.print(pgConnection);
String vCode = request.getParameter("selectedVCode");
String fromDate = request.getParameter("selectedFromDate");
// out.print(fromDate);
// out.print(vCode);
String sql = "";
sql += "SELECT v.vehiclecode, v.employeecode, e.name, e.employeecategory";
sql += " FROM hoobtvehcileemplyees v, hoobtemplyees e";
sql += " WHERE v.employeecode = e.employeecode AND";
sql += " v.vehiclecode = " + vCode + " AND v.datefrom < '" + fromDate + "' AND v.dateto > '" + fromDate + "'";
defaultSet = statement.executeQuery(sql);
String jsonData = "{'defaultVal':[";
while (defaultSet.next()) {
jsonData += "{ 'category' : '";
jsonData += defaultSet.getString(4).trim();
jsonData += "', ";
jsonData += "'name' : '";
jsonData += defaultSet.getString(3).trim();
if (defaultSet.isLast()) {
jsonData += "' } ";
} else {
jsonData += "' } , ";
}
}
jsonData += "]}";
out.print(jsonData);
} catch (Exception e) {
// TODO Auto-generated catch block
out.println(e.toString());
e.printStackTrace();
}
}
}
这是我的Ajax响应。
{'defaultVal':[{ 'category' : '1', 'name' : 'Kamal Subhasingha' } , { 'category' : '2', 'name' : 'Namal Witharana' } , { 'category' : '3', 'name' : 'Yohan' } , { 'category' : '4', 'name' : 'Ahan Liyanage' } , { 'category' : '5', 'name' : 'Sampath Jayaweera' } , { 'category' : '6', 'name' : 'Saman' } ]}
现在我需要检索关于类别3和类别5的名称字段。我应该将这些值设置为我的组合框值(我有两个组合框)。我试过跟随。
var jsonData = Ext.util.JSON.decode(response.responseText);
console.log(jsonData);
alert(jsonData.name);
它打印jsonData。但它给出了一个未定义的警报。
我的代码出了什么问题?有人有想法吗?
如果有人愿意解释我的代码出了什么问题以及我该怎么做这样的话,我会非常感激。
谢谢
答案 0 :(得分:1)
您忘记了包含其他对象的 defaultVal 参数。
console.log(jsonData.defaultVal);
您可以访问'name'属性