【问题标题】:jquery sends form data on post request but when i modify servlet to return json & modify jquery to accept json servlet is not receiving nulljquery 在 post 请求上发送表单数据,但是当我修改 servlet 以返回 json 并修改 jquery 以接受 json 时,servlet 没有收到 null
【发布时间】:2015-01-15 11:07:08
【问题描述】:

我的即时消息将 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){
			
		}
		
		
		

【问题讨论】:

    标签: java jquery json jsp servlets


    【解决方案1】:

    我找到了解决方案,伙计们... 我用这个 application/x-www-form-urlencoded 替换了内容类型,它现在正在工作...... 谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多