87,909
社区成员
发帖
与我相关
我的任务
分享
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [ o[this.name] ];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
$(document).ready(
function() {
jQuery.ajax( {
type : 'GET',
url : 'jasonServlet',
dataType: 'json', //返回值类型
success : function(data) {
//if (data && data.success == "true") {
if (data) {
alert(data);
$.each(data, function(i, item) {
$('#info').html("共" + item.total + "条数据。<br/>"+item.success+"<br/>");
// $.each(data.data, function(i, item) {
$.each(item.data, function(i, item) {
$('#info').append(
"编号:" + item.id + ",姓名:" + item.username + ",年龄:" + item.age);
});
});
}
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
}
});
$("#submit").click(function() {
var jsonuserinfo = $.toJSON($('#form').serializeObject());
alert(jsonuserinfo);
jQuery.ajax( {
type : 'POST',
contentType : 'application/json',
url : 'http://localhost:8081/TPVFrames/CheckUserLogin',
data : jsonuserinfo,
dataType : 'json',
success : function(data) {
alert("新增成功!");
},
error : function(data) {
alert("error");
}
});
});
});
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Spring MVC</title>
<script type="text/javascript" src="javascript/jquery-1.8.3.js"></script>
<script type="text/javascript" src="javascript/json2.js"></script>
<script type="text/javascript" src="javascript/for_jQuery_ajax_servlet.js"></script>
</head>
<body>
<div id="info"></div>
<form action="add" method="post" id="form">
id:<input type="text" name="id"/>
name:<input type="text" name="username"/>
age:<input type="text" name="age"/>
<input id="submit" type="button" value="提交"/>
</form>
</body>
</html>
public class CheckUserLogin extends HttpServlet {
private static final long serialVersionUID = 1L;
public CheckUserLogin() {
super();
}
public void init() throws ServletException {
// Put your code here
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String userStr = readJSONString(request);// 得到requestContext
JSONObject jsonObj = JSONObject.fromObject(userStr);// 转换成JSONObject
System.out.println(jsonObj.getInt("id"));// 得到JSONObject的userId值
System.out.println(jsonObj.getString("username"));
System.out.println(jsonObj.getString("age"));
JSONObject resultJSON = new JSONObject();// 构建一个JSONObject
resultJSON.accumulate("errNum", 1);
resultJSON.accumulate("errInfo", "成功");
response.setContentType("application/x-json");// 需要设置ContentType为"application/x-json"
PrintWriter out = response.getWriter();
out.println(resultJSON.toString());// 向客户端输出JSONObject字符串
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public String readJSONString(HttpServletRequest request) {
StringBuffer json = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null) {
json.append(line);
}
} catch (Exception e) {
System.out.println(e.toString());
}
return json.toString();
}
<body>
<form name="nameForm" id="nameForm" method="post" action="javascript:login()">
email:<input id="email" type="text" name="email"/>
<input id="submit" type="submit" value="提交"/>
</form>
</body>
</html>
<script type="text/javascript">
function login(){
$("#loading").hide();
var email = document.nameForm.email.value;
var formdata = $('#nameForm').serialize();
alert(formdata);
$.ajax({
type: "GET",
url: "ProcessFormServlet",
data: formdata,
success: function(result){
alert(result);
}
});
}
</script>
Servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
String emailname = request.getParameter("email");
System.out.println(emailname);
PrintWriter out = response.getWriter();
out.println(emailname);
}
可传email的value后由Servlet返回再由jQuery显示email
$("#submit").click(function() {
// var jsonuserinfo = $.toJSON($('#form').serializeObject());
// alert(jsonuserinfo);
jQuery.ajax( {
type : 'POST',
contentType : 'application/json',
url : 'http://localhost:8080/TPVFrames/for_jQuery_ajax_servlet.jsp',
data :$('#form').serialize(),
dataType : 'json',
success : function(data) {
alert("新增成功!");
},
error : function(data) {
alert("error");
}
});
}
//var jsonuserinfo = $.toJSON($('#form').serializeObject());
// alert(jsonuserinfo);
jQuery.ajax( {
type : 'POST',
contentType : 'application/json',
url : 'http://localhost:8081/TPVFrames/CheckUserLogin',
data :$('#form').serialize()// jsonuserinfo,///直接这样就好了。。
dataType : 'json',
success : function(data) {
alert("新增成功!");
},
error : function(data) {
alert("error");
}
});