我发送jquery到servlet并尝试连接db。它运作良好。 但是当我尝试从servlet返回json并修改jsp以接受json类型时。 servlet收到的参数为null为什么? 等待解决方案...
这是我的代码:
function submitData()
{
alert('Called');
$('document').ready(function(){
$.ajax({
url:'/DashBoard/FetchAlerts',
type:'post',
dataType:'json',
data: $('#dataForm').serialize(),
cache: false,
contentType: 'application/json',
success: function(data) {
alert('Yes!');
$.each(data.obj,function(index,obj)
{
alert('Date:'+obj.date+'Hour:'+obj.hours);
})
}
});
});
}
如果我删除行数据类型和contentType或将contentType设置为application / www-x-urlencoded servlet正在接收参数。
这是我的servlet代码:
response.setContentType("application/json");
int startHour,startMinute,endHour,endMinute;
String temp[];
Calendar cal;
out=response.getWriter();
System.out.println("Url Called");
String startDate=request.getParameter("startDate");
System.out.println(request.getParameter("startDate"));
System.out.println(request.getAttribute("startDate"));
Timestamp startTDate,endTDate;
if(startDate==null||startDate.trim().isEmpty())
{
startDate=new Date()+"";
}
String endDate=request.getParameter("endDate");
if(endDate==null||endDate.trim().isEmpty())
{
endDate=new Date()+"";
}
String color=request.getParameter("color");
if(color==null||color.trim().isEmpty())
{
color="Red";
}
String startTime=request.getParameter("startTime");
if(startTime==null||startTime.trim().isEmpty())
{
startTime=new Date().getTime()+"";
}
temp=startTime.split(" ");
temp=temp[0].split(":");
try{
startHour=Integer.parseInt(temp[0]);
}catch(NumberFormatException ne){
startHour=0;
}
if(startHour+""=="12")
startMinute=Integer.parseInt(temp[1]);
else
startMinute=Integer.parseInt(temp[1])+12;
String endTime=request.getParameter("endTime");
if(endTime==null||endTime.trim().isEmpty())
{
endTime=new Date().getTime()+"";
}
temp=endTime.split(" ");
temp=temp[0].split(":");
try{
endHour=Integer.parseInt(temp[0]);
}catch(NumberFormatException ne){
endHour=0;
}
if(endHour+""=="12")
endMinute=0;
else
endMinute=Integer.parseInt(temp[1]);
String searchText=request.getParameter("searchTextBox");
if(searchText.trim().isEmpty())
{
searchText="";
}
String radio1=request.getParameter("radio1");
if(radio1.trim().isEmpty())
{
radio1="All";
}
System.out.println("Status:"+radio1);
try{
SimpleDateFormat from=new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat to=new SimpleDateFormat("yyyy-MM-dd");
Date date=from.parse(startDate);
System.out.println("Date:"+date);
startDate=to.format(date);
cal=GregorianCalendar.getInstance();
cal.set(Calendar.DATE, date.getDate());
cal.set(Calendar.MONTH, date.getMonth());
cal.set(Calendar.YEAR, date.getYear()+1900);
cal.set(Calendar.HOUR, date.getHours());
cal.set(Calendar.MINUTE, date.getMinutes());
startTDate=new Timestamp(cal.getTimeInMillis());
date=from.parse(endDate);
System.out.println("Date1:"+date);
cal=GregorianCalendar.getInstance();
cal.set(Calendar.DATE, date.getDate());
cal.set(Calendar.MONTH, date.getMonth());
cal.set(Calendar.YEAR, date.getYear()+1900);
cal.set(Calendar.HOUR, date.getHours());
cal.set(Calendar.MINUTE, date.getMinutes());
endTDate=new Timestamp(cal.getTimeInMillis());
endDate=to.format(date);
sql="select * from alerts where Date BETWEEN '"+startTDate+"' AND '"+endTDate+"' AND color='"+color+"'AND Status='"+radio1+"'";
}
catch(ParseException p){
}
catch(NullPointerException ne){
}
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
JSONObject json=new JSONObject();
JSONArray obj;
obj=new JSONArray();
while(rs.next()){
JSONObject ob1=new JSONObject();
Timestamp tem=rs.getTimestamp("Date");
SimpleDateFormat s1=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try{
Date d1=s1.parse(tem+"");
int hour=d1.getHours();
int min=d1.getMinutes();
s1=new SimpleDateFormat("dd-MM-yyyy");
ob1.put("Date", d1);
ob1.put("Hours",hour);
ob1.put("Minutes",min);
ob1.put("Status", rs.getString("Status"));
ob1.put("Color",rs.getString("Color"));
ob1.put("Description", rs.getString("Description"));
}catch(ParseException p){
}
catch(JSONException je){
}
}
json.put("obj", obj);
out.print(json);
rs.close();
stmt.close();
conn.close();
}
catch(SQLException sq){
}
catch(ClassNotFoundException ce){
}
catch(JSONException je){
}
答案 0 :(得分:1)
我找到解决方案的人...... 我用这个应用程序/ x-www-form-urlencoded替换了内容类型,它现在正在工作...... 谢谢