weixin_33691598 2015-09-13 16:22 采纳率: 0%
浏览 37

jQuery ajax返回JSON

I have a problem with my code. I don't understand what is the problem, I hope that you give me help. I have this servlet code:

protected void doPost(HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  PrintWriter out = response.getWriter();

  String from = request.getParameter("p1"); 
  String to = request.getParameter("p2");
  String titolo = request.getParameter("p3");

  Date date;
   double price;
   String dateString, priceString;
  String str= "";

  try{
       Class.forName("com.mysql.jdbc.Driver");
       Connection connection = DriverManager.getConnection(".......");
       Statement statement = connection.createStatement();
       String query = "SELECT f.titolo, PRO.data, SUM(CASE WHEN tipobiglietto = 'intero' THEN 1 ELSE 0 END) as numeroInteri, "+
                      "                           SUM(CASE WHEN tipobiglietto = 'ridotto' THEN 1 ELSE 0 END) as numeroRidotti "+
                      "FROM film f join proiezione PRO on f.idfilm = PRO.idfilm join prenotazione PRE on PRE.idproiezione = PRO.idproiezione "+ 
                      "WHERE f.titolo ='"+titolo+"' AND PRO.data BETWEEN '"+from+"' AND '"+to+"' "+
                      "GROUP BY PRO.data "+
                      "ORDER BY f.idfilm";

       ResultSet rs = statement.executeQuery(query);
       // Se il result set è vuoto: chiudo la stringa che contiene la sintassi JSON
       if(rs.next() != true){
          str +=  " " ;
       }

       //Se il result set è pieno: riempo la stringa contenente la sintassi JSON
       else{
          rs.beforeFirst();
          while(rs.next()){
            date = rs.getDate("data");
             price = rs.getDouble("numeroInteri")*(8.00)+rs.getDouble("numeroRidotti")*(5.00);
            dateString = date.toString();
             priceString = Double.toString(price);
             if(rs.isLast()){
               str += "{ \"data\":\""+dateString+"\" , \"incasso\":\""+priceString+"\" }";
             }
             else{
               str += "{ \"data\":\""+dateString+"\" , \"incasso\":\""+priceString+"\" },";
             }
          }
             connection.close();
            rs.close();
            statement.close();
            out.println(" { \"info\" : ["+str+" ]} ");
          }
  }
          catch (Exception e){
             System.out.print("Do not connect to DB - Error:"+e);
          }

  }

}

And this is ajax method

   $.ajax({
         type: "POST",
         url: "adminFilmInformation",
         data: {p1:fieldFrom, p2:fieldTo, p3:fieldTitolo},
         success: function(response){
            var obj = JSON.parse(response);
                           /*Some code*/
         },
         error: function(xhr,status){
            alert(status);
         }
      });

$.ajax() show me an alert whit error message. I try to solve the problem, but i don't find a solution. So, please give me some help. I apologize for my English, because I'm an Italian boy XD. Thank you for help.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 STM32单片机自主设计
    • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
    • ¥15 不小心不正规的开发公司导致不给我们y码,
    • ¥15 我的代码无法在vc++中运行呀,错误很多
    • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
    • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
    • ¥30 ppOCRLabel导出识别结果失败
    • ¥15 Centos7 / PETGEM
    • ¥15 csmar数据进行spss描述性统计分析
    • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了