I know this question has been asked before but i am unable to get the solution this is the file from where I am sending json data
我知道之前有人问过这个问题,但我无法得到解决方案这是我发送json数据的文件
<%
//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();
%>
On this file I amm trying to access data like this
在这个文件上,我试图访问这样的数据
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);
}
I have tried setting dataType:json too but then nothing is working,I am getting undefined everytime This is the data which I am getting through the server response,form my jspfile
我也尝试过设置dataType:json,但是什么都没有工作,每次这是我通过服务器响应获得的数据,形成jspfile时,我都没有定义
{"image":7,"date_t":2018-06-14,"comments":"NA","staff_id":1,"id":100,"status":1}
1 个解决方案
#1
1
Output your data like
输出数据等
JSONArray jp = new JSONArray();
jp.put(result);
out.println(jp);
out.flush();
And then in javascript
然后在javascript
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);
}
Use dataType:json it will automatically parse data ,I hope it will work.
使用数据类型:json会自动解析数据,我希望它能起作用。
#1
1
Output your data like
输出数据等
JSONArray jp = new JSONArray();
jp.put(result);
out.println(jp);
out.flush();
And then in javascript
然后在javascript
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);
}
Use dataType:json it will automatically parse data ,I hope it will work.
使用数据类型:json会自动解析数据,我希望它能起作用。