首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从jsp访问javascript中的json数据?

如何从jsp访问javascript中的json数据?
EN

Stack Overflow用户
提问于 2018-07-24 07:19:47
回答 1查看 0关注 0票数 0

这是我发送json数据的文件:

代码语言:txt
复制
<% 


//JSONArray arrayObj = new JSONArray();
//JSONArray data = new JSONArray();

int count = 0;
 JSONObject result = new JSONObject();



try{


        Connection conn = null;
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        conn = DriverManager.getConnection(
                "jdbc:mysql://localhost/schoolproject","root", "");
                Statement stmt = conn.createStatement();


PreparedStatement ps=conn.prepareStatement("select * from staff_attendance where id=?");
ps.setInt(1,Integer.valueOf(request.getParameter("id")));


      ResultSet rs = ps.executeQuery();


      while(rs.next())
      {
      count++;
      JSONArray ja = new JSONArray();
      Map<String,Integer> map=new HashMap<String,Integer>();  
       //ja.put("<input type='checkbox' name='selected' value="+rs.getInt("id")+">");
       result.put("id",rs.getInt("id"));
       //map.put("id",rs.getInt("id"));

      result.put("staff_id",rs.getInt("staff_id"));
       //map.put("staff_id",rs.getInt("staff_id"));

      result.put("date_t",rs.getDate("date_t"));
       result.put("status",rs.getInt("status"));
       //map.put("status",rs.getInt("status"));
       result.put("comments",rs.getString("comments"));
       result.put("image",rs.getInt("image"));
       //ja.put("<button type='button' id='"+rs.getInt("id")+ "'class='btn mine btn-danger btn-sm' onclick='ajaxcall(this.id)'><span class='glyphicon glyphicon-pencil'></span> Edit </button>");
       //ja.put(map);
       //data.put(ja);
       //break;
      //out.println(arrayObj);
      //out.println(",");



}
      }
      catch(Exception e){
        out.println(e);

        }


//result.put("aaData", data);


   //out.println(result);    
   out.println(result.toString());
 out.flush();


        %>

在这个文件中,我试图访问这样的数据

代码语言:txt
复制
function ajaxcall(id){
    $.ajax({
   type:"post",
   url:"edit.jsp",
   data:{id:id},
   success:function(e){
       //var obj = JSON.parse(e)
       //$("#myid").val(obj[0].id);
   //alert(e);
   alert(e);
   console.log(e[id]);
   console.log(e.id);

       }

        });
   //alert(id);

}

我也尝试过设置dataType:json,但是无法实现,我得到了undefined。每当这是我通过服务器响应得到的数据时,形成我的jsp文件。

代码语言:txt
复制
{"image":7,"date_t":2018-06-14,"comments":"NA","staff_id":1,"id":100,"status":1}
EN

回答 1

Stack Overflow用户

发布于 2018-07-24 16:49:57

输出数据,如下所示

代码语言:txt
复制
JSONArray jp = new JSONArray();
jp.put(result);
out.println(jp);
 out.flush();

然后在javascript中

代码语言:txt
复制
function ajaxcall(id){
    $.ajax({
   type:"post",
   url:"edit.jsp",
   dataType: 'json',
   data:{id:id},
   success:function(e){
       //var obj = JSON.parse(e)
       //$("#myid").val(obj[0].id);
   //alert(e);
   alert(e[0]);
   console.log(e[0].id);

       }

        });
   //alert(id);

}

使用dataType:json,它将自动解析数据,我希望它能工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档