JavaScript使用ajax传递表单数据:
$.ajax({url : "getValue",method : "post",traditional:true,cache : false,async : false,data : {tel : tel,pwd : pwd},dataType : "json",beforeSend : function() {//alert("before send");},success : function(data) {var json = JSON.stringify(data);var a = eval('(' + json + ')');//转json字符串,不可以为 eval(json)alert(a.x);//获取数据 alert(a.y);//获取数据},error : function(data) {}})
JAVA传递JSON对象给JSP页面内的JavaScript:
try {// 将后台信息传至前台PrintWriter out = response.getWriter();JsonObject jsonObject=new JsonObject();jsonObject.addProperty("x", "我是第一个字符串");jsonObject.addProperty("y", "我是第二个字符串");out.println(jsonObject);out.flush();out.close();} catch (IOException e) {e.printStackTrace();}
导入的jar包:
HTML页面用ajax方式传递表单数据给后台 数据库插入信息后将json对象传回前端JS处理(前后台交互)