简体   繁体   English

jQuery在请求后发送表单数据,但是当我修改servlet以返回json并修改jQuery以接受json servlet时未收到null

[英]jquery sends form data on post request but when i modify servlet to return json & modify jquery to accept json servlet is not receiving null

My im sending jquery to a servlet and try to connect with db. 我将jQuery发送到servlet并尝试与数据库连接。 it is working good. 运行良好。 But when I try to return json from servlet and modify jsp to accept json type. 但是当我尝试从servlet返回json并修改jsp以接受json类型时。 the parameters received by servlet are null why?? servlet接收的参数为null为什么? Waiting for the solution... 等待解决方案...

Here is my code: 这是我的代码:

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);
                    })
            } 
            });
    });
}

if i remove lines datatype and contentType or set contentType to application/www-x-urlencoded servlet is receiving parameters. 如果我删除行数据类型和contentType或将contentType设置为application / www-x-urlencoded servlet正在接收参数。

Here is my servlet code: 这是我的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){ } 

我找到了解决方案的人...我用此应用程序/ x-www-form-urlencoded替换了内容类型,它现在正在工作...谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM