Jsp 使用json对象重定向
我的UI中有两个组合框。第一个组合框包含国家名称列表,第二个组合框包含该国家的国家列表。从第一个组合中,当我选择国家时,名称被发送到servlet。使用该名称,将命中数据库,检索状态名称列表并将其转换为JSONObject。现在,我无法将这个JSONObject传递回extjs文件,以便用状态列表填充第二个组合框 以下是js文件的代码:Jsp 使用json对象重定向,jsp,servlets,extjs,extjs4,extjs4.1,Jsp,Servlets,Extjs,Extjs4,Extjs4.1,我的UI中有两个组合框。第一个组合框包含国家名称列表,第二个组合框包含该国家的国家列表。从第一个组合中,当我选择国家时,名称被发送到servlet。使用该名称,将命中数据库,检索状态名称列表并将其转换为JSONObject。现在,我无法将这个JSONObject传递回extjs文件,以便用状态列表填充第二个组合框 以下是js文件的代码: Ext.require('Ext.tab.*'); Ext.require('Ext.button.*'); Ext.define('Country'
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是包含两个组合框的文件。运行此代码时,我收到带有“failure”消息的警报
它告诉我们:这一行的语法错误:
var respText = eval('('+ obj1.responseText +')');
请告诉我如何解决这个问题。我想到了两个想法 首先,为什么使用POST而不是GET 第二,尝试使用以下内容设置请求/响应头:
'Accept' : 'application/json'
'Content-type' : 'application/json'
var respText=Ext.JSON.decode(obj.responseText) 尝试:
var respText=Ext.JSON.decode(obj.responseText)
得到以下信息:未捕获异常:Ext.JSON.decode():您正在尝试解码无效的JSON字符串:
我的第一个想法是您打印的JSON无效,您是否尝试查看obj.responseText中的内容?你能发布那个值吗?当我尝试alert(obj.responseText)
时,我没有收到任何消息。在java端,JSON看起来是这样的:{“data”:[{“state”:“Shanghai”},{“state”:“Hangzou”},{“state”:“Dali”}]}
如果是这种情况,那么您的JSON不会被视为无效,我们需要查看您的实际JSON,您可以从网络面板中的响应中获得它(如果您使用FF,则在Firebug上;如果您使用Chrome,则在开发人员控制台中)。我猜您的服务器端JSON没有正确引用,或者是其中的某行。请阅读文档中的以下信息:`“GET”(如果没有发送参数),以及“POST”如果正在发送参数,因为我将国家名称作为请求中的参数发送,因此使用POSTyes,当然您也可以使用get发送数据。Ext.Ajax.request({url:'/cellbediting/FormServlet?cOuntry='+obj.getValue(),方法:'get',…