springmvc 使用json交互数据 (ajax)

本文介绍了一个使用SpringMVC框架实现的简单JSON交互示例。示例中包括了一个后端控制器StoreQueryAction,该控制器通过@RequestBody接收前端发送的JSON格式的数据,并通过@ResponseBody返回处理后的JSON响应。此外,还提供了一个前端页面,该页面使用jQuery发起AJAX请求与后端进行通信。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spirngmvc默认使用的是MappingjacksonHttpMessageConverter对json数据进行转换,需要加入

jackson-core-asl.jar

jackson-mapper-asl.jar 

jackson-databind.jar

jackson-core.jar

jackson-annotations.jar

然后使用@RequestBody  @ResponseBody这两个注解。

public class StoreQueryAction {

	/**
	 * 提交登陆页面
	 * @param i
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/login")
	public @ResponseBody Map<String, String> login(String username, String password) throws Exception {
		// TODO Auto-generated method stub	
		
		Map<String, String> map = new HashMap<String, String>();	
		map.put("code", "haha");
		map.put("enen", "发发");
		
		return map;
	}

	
}

前台页面

<!--Author: W3layouts
Author URL: http://w3layouts.com
License: Creative Commons Attribution 3.0 Unported
License URL: http://creativecommons.org/licenses/by/3.0/
-->
<!DOCTYPE HTML>
<html>
<head>
<title>乌云后台登录</title>
<!-- Custom Theme files -->
<link href="css/loginstyle.css" rel="stylesheet" type="text/css" media="all"/>
<!-- Custom Theme files -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<meta name="keywords" content="后台登录" />

<script type="text/javascript" src="js/jquery/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
	function login() {
		var userdata = {
            "username":$("#username").val(),
            "password":$("#password").val()};
		$.ajax({
			url : "login.do",
			method : "post",
			dataType : "json",
			data : {"username":"ssss", "password":"ccccc"},
			success : function(data){
				var d = JSON.stringify(data);
				if (d.code == "100") {
					window.location.href = "jsp/main.jsp";
				}else{
					var msg = d.msg;
					document.getElementById("error").innerHTML = msg;
				}
			}
		});
	
	}
</script>

</head>
<body>
<!--header start here-->
<div class="login-form">
			<div class="top-login">
				<span><img src="img/group.png" alt=""/></span>
			</div>
			<h1>塔南店信息查系统</h1>
			<div class="login-top">
			<form>
				<div class="login-ic">
					<i ></i>
					<input type="text" id="username" value="用户" onFocus="this.value = '';" onBlur="if (this.value == '') {this.value = 'User name';}"/>
					<div class="clear"> </div>
				</div>
				<div class="login-ic">
					<i class="icon"></i>
					<input type="password" id="password" value="密码" onFocus="this.value = '';" onBlur="if (this.value == '') {this.value = 'password';}"/>
					<div class="clear"> </div>
				</div>
			
				<div class="log-bwn">
					<input type="submit" value="Login" onclick="login()">
				</div>
				</form>
				<div id="error"></div>
			</div>

</div>		
<!--header start here-->
</body>
</html>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值