form表单数据提交-ajax

具体看代码就明白了

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>form表单数据提交-ajax</title>
</head>
	<body>
		<form  method="post" onsubmit="return false" action="##" id="form">
			username:<input type="text" name="username" /><br>
			password:<input type="password" name="password" /> <br>
			<input type="button" value="登录" onclick="login()">
		</form>
		<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
		<script type="text/javascript">
			function login() {
				$.ajax({
					type: "POST",
					dataType: "text",
					/*
						xml:返回XML文档,可用JQuery处理。
						html:返回纯文本HTML信息;包含的script标签会在插入DOM时执行。
						script:返回纯文本JavaScript代码。不会自动缓存结果。除非设置了cache参数。注意在远程请求时(不在同一个域下),所有post请求都将转为get请求。
						json:返回JSON数据。
						jsonp:JSONP格式。使用SONP形式调用函数时,例如myurl?callback=?,JQuery将自动替换后一个“?”为正确的函数名,以执行回调函数。
						text:返回纯文本字符串。
					*/
					url: "http://localhost:8080/user",
					data: $('#form').serialize(),//jQuery的serialize()方法通过序列化表单值,输出类似的值:username=1&password=2
					success: function (result) {
						alert(result);
					},
					error : function(error) {
						alert(error);
					}
				});
			}
		</script>
	</body>
</html>
发布了90 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/me_never/article/details/103470152
今日推荐