问题描述
- springmvc ajax json
-
刚开始学ajax。。。导入的jar包:jackson-mapper-asl-1.8.8.jar 和 jackson-core-asl-1.8.8.jarjsp代码
<head> <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.9.1.js"></script> <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.9.1.min.js"></script> <meta http-equiv="Content-Type" content="text/html; charset=utf8"> <title>Insert title here</title> <script type="text/javascript"> function ajaxTest(){ $.ajax({ data:"name="+$("#name").val(), //参数 type:"GET", dataType: 'json', //响应数据类型 url:"${pageContext.request.contextPath}/login/ajax.html", dataFilter:function(data,type) {console.log(data); console.log(type); }, error:function(data){ alert("出错了!!:"+data.msg); console.log(arguments); }, success:function(data){ alert("success:"+data.msg); $("#result").html(data.msg) ; console.log(arguments); } }); } </script> </head> <body> <input type="text" name="name" id="name"/> <input type="submit" value="登录" onclick="ajaxTest();"/> <div id="result"></div> </body>
controller代码:
@RequestMapping(value="ajax",method=RequestMethod.GET) public @ResponseBody Map<String,Object> login(HttpServletRequest request,HttpServletResponse response) throws IOException{ System.out.println(request.getParameter("name")); Map<String,Object> map = new HashMap<String,Object>(); if(request.getParameter("name").equals("123")){ System.out.println("成功"); map.put("msg", "成功"); }else{ System.out.println("失败"); map.put("msg", "失败"); } return map; }
出现的错误
解决方案
靠人不如靠己啊,在stackoverflow看到了,把 url:"${pageContext.request.contextPath}/login/ajax.html", 改成 url:"${pageContext.request.contextPath}/login/ajax.json",就解决了
解决方案二:
SpringMVC ajax JSON 中文乱码问题
SpringMVC ajax JSON 中文乱码问题
SpringMVC+ajax返回JSON串
时间: 2024-09-29 23:06:48