(controller与ajax之间的数据传输)关于controller无法返回json类型参数回ajax中的问题
2019-05-09 16:10
441 查看
最近在写一个基于ssm框架的javaWeb项目,第一次使用ssm框架,遇到了controller无法将json返回页面的ajax中,昨天下午得到了解决,现在在这里做一下记录。
首先要导入json的三个架包:
下载地址:https://github.com/Lyr0422/llyr/tree/master/jackson
接下来配置springmvc.xml配置文件,配置json格式数据转换
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "> <context:component-scan base-package="com.shopWatch.controller" /> <mvc:annotation-driven /> <mvc:resources location="/css/" mapping="/css/**" /> <mvc:resources location="/img/" mapping="/img/**" /> <mvc:resources location="/js/" mapping="/js/**" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" /> <!--json格式数据转换的配置 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" p:ignoreDefaultModelOnRedirect="true"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> </list> </property> </bean> </beans>
然后就是controller与ajax部分的编写了,需注意的是,在controller类中的方法上面要加
@ResponseBody注解。
实现简单登录验证的controller层代码:
@RequestMapping(value = "userLogin.action", method = RequestMethod.POST) @ResponseBody public Map<String, String> UserLogin(User user,HttpServletRequest request) { Map<String, String> rel = new HashMap<String, String>(); User isUser=userService.UserLogin(user); if(isUser==null) { rel.put("type", "error"); rel.put("msg", "用户名或密码错误"); return rel; } rel.put("type", "success"); rel.put("msg", "登录成功!"); return rel; }
在jsp页面中的ajax代码:
<input type="button" id="ok" name="ok" value="登录" onclick="login()"/>
function login(){ var username=$("#userName").val(); var password=$("#passWord").val(); $.ajax({ url:'${pageContext.request.contextPath}/userLogin.action', data:{username:username,password:password}, type:'post', dataType:'json', success:function(data){ if(data.type=="success"){ alert(data.msg); window.location.href="${pageContext.request.contextPath}/findAllGoods.action" }else{ alert(data.msg); $("#passWord").val(""); } } }) }
相关文章推荐
- dhl:ajax无法跨域改用getJSON(解决服务器返回json数据中文为乱码的问题$.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/json; chars)
- 关于 Ajax 提交参数格式,及返回类型json
- 关于 Ajax中返回json类型数据为什么使用? eval()
- Ajax前台返回JSON数据后再Controller中直接转换成类型使用,后台接收json转成实体的方法
- 关于jquery中ajax数据返回类型问题
- 使用ajax返回json类型数据的时候无法进入success可能的原因
- ajaxfileupload以JSON为返回数据类型出现的因符号无法解析的错误的解决方案
- 关于ajax 返回的json数据动态展现在jsp页面的问题 20170525
- 关于传入不同参数请求同一Ajax后返回数据显示错乱的问题
- ajaxFileupload.js修复上传无法携带参数,返回json格式不准确问题
- ajaxfileupload以JSON为返回数据类型出现的因符号无法解析的错误的解决方案
- .net 关于JSON转换为对象时出现的:数据协定类型 “***”无法反序列化,因为未找到必需的数据成员 **(字段名)
- js、java、jsp页面三者之间的传值问题,包括ajax的返回参数
- 关于webwork框架返回JSON数据格式的问题2015-10-27
- 关于js处理ajax请求返回json数据时出现页面缓存的解决方案(适用于showdialog)
- $.ajax返回的JSON格式的数据后无法执行success的解决方法
- ajax实现返回数据是html类型的跨域问题
- 用JS解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题
- jQuery调用WebService返回JSON数据及参数设置注意问题
- Struts2+Jquery实现ajax并返回json类型数据