我提交了一段数据,然后跳转到SERVLET中执行后会得到7个左右的数据,我想把他返回回来并且能够在页面显示出来,请高人指点下,以下是我的代码:
JSP页面:
<body>
<center>
<table border="1">
<form action="" >
<tr>
<td>
<font >请输入数字:</font> <br>
<input type="text" id="pin" name="pincode" value="" maxlength="13" onkeypress="enterDown()" />
<input type="submit" value="提交" id="subfind">
</td>
</tr>
</form>
</table>
<div id="dataDiv"></div>
<br>
<a href="Login.jsp">返回到登陆界面</a>
</center>
</body>
JS代码:
function enterDown () {
if(event.keyCode=="13")event.returnValue = false;
}
$(document).ready(function(){
$("#subfind").click(function(){
var pin = $("#pin").val();
if (pin.length < 13){
alert("请输入PIN");
return
}
else {
$.ajax({
type: "post",
url: "CheckPinCodeServlet",
data: $('form').serialize(),
success: function(data){
var jsonvars = data.mydata;
alert(jsonvars.length);//我在这里想用alert测试下有没有取到值,但是发现没有值。。
for (var i = 0 ; i < jsonvars.length ; i++) {
$("#dataDiv").append+"<div>"+jsonvars[i].pin1+jsonvars [i].name1+jsonvars[i].address+jsonvars[i].postcode1+jsonvars[i].mobilephone1+jsonvars[i].telephone1+jsonvars[i].idcard1+jsonvars[i].remark1+"</div>";
}
}
});
}
});
});
servlet中的代码:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String pin = request.getParameter("pincode");
JSONObject json = new JSONObject();
JSONObject member = new JSONObject();
UserInfo userInfo = new UserInfo();
UserDao userDao = new UserDao();
userDao = userInfo.isExistInfo(pin);
member.put("pin1", pin);
member.put("name1", userDao.getName());
member.put("address1", userDao.getAddress());
member.put("postcode1", userDao.getPostcode());
member.put("mobilephone1", userDao.getMobilePhone());
member.put("telephone1", userDao.getTelePhone());
member.put("idcard1", userDao.getIdCard());
member.put("remark1", userDao.getRemark());
json.put("mydata", member);
PrintWriter out = response.getWriter();
out.print(json.toString());
out.close();
}
想请问下我这个哪里有问题了?userdao的值我试过都是有的~~请高人指点我下哪里有问题?怎么修改额?
------解决方案--------------------------------------------------------
- Java code
UserInfo user = new UserInfo();user.setName("ludwik"); JSONObject json = JSONObject.fromObject(user);
------解决方案--------------------------------------------------------
强烈建议chrome 或者 FF 浏览器进行debug,这种问题一眼看不出来,再说代码很多 很难定位错误。
http://www.happyalaric.com
------解决方案--------------------------------------------------------
UserInfo userInfo = new UserInfo();
UserDao userDao = new UserDao();
userDao = userInfo.isExistInfo(pin);
你这几行代码是怎么回事?UserInfo不是bean?
UserDao不应该是数据方位层代码吗?
userDao = userInfo.isExistInfo(pin);
你这行让我百思不得其解。
又看了你这几行代码
member.put("pin1", pin);
member.put("name1", userDao.getName());