@ResponseBody返回4种数据格式的数据

1、返回一个键值对或者集合

前端JS请求:

  1. //返回值为map的形式
  2. $(".name").blur(function(){
  3. $.ajax({
  4. type:"Post",//请求类型
  5. url:"/mvc-demo/user/nameProving?t="+new Date().getTime(),//请求的url
  6. data:{name:$(".name").val()},//请求参数
  7. dataType:"json",//ajax接口(请求url)返回的数据类型
  8. success:function(data){//data:返回数据(json对象)
  9. if(data.name == "empty"){//name为空,错误提示
  10. $(".errorFont").text("用户名为不能为空!");
  11. $(".errorFont").css("color","red");
  12. }else if(data.name == "exist"){
  13. $(".errorFont").text("用户名已注册");
  14. $(".errorFont").css("color","green");
  15. }else if(data.name == "noexist"){
  16. $(".errorFont").text("用户名未注册");
  17. $(".errorFont").css("color","red");
  18. }
  19. },
  20. error:function(data){
  21. $(".errorFont").text("发生未知错误,请联系管理员!");
  22. $(".errorFont").css("color","red");
  23. }
  24. });
  25. });

后端逻辑处理:

  1. //验证用户是否存在,返回一个键值对的数据
  2. @RequestMapping("/nameProving")
  3. @ResponseBody
  4. public Object nameProving(String name){
  5. HashMap<String,String> resultMap=new HashMap<String,String>();
  6. if(StringUtils.isEmpty(name)){
  7. resultMap.put("name", "empty");
  8. }else{
  9. Userss user=userService.getUserByName(name);
  10. if(user!=null)
  11. resultMap.put("name", "exist");
  12. else
  13. resultMap.put("name", "noexist");
  14. }
  15. return JSONArray.toJSONString(resultMap);
  16. }

2、返回一个JSON格式的String字符串

前端js请求:

  1. //返回值为String的形式,dataType:"json"
  2. $(".name").blur(function(){
  3. $.ajax({
  4. type:"Post",//请求类型
  5. url:"/mvc-demo/user/nameProving2?t="+new Date().getTime(),//请求的url
  6. data:{name:$(".name").val()},//请求参数
  7. dataType:"json",//ajax接口(请求url)返回的数据类型
  8. success:function(data){//data:返回数据(json对象)
  9. if(data == "empty"){//name为空,错误提示
  10. $(".errorFont").text("用户名为不能为空2!");
  11. $(".errorFont").css("color","red");
  12. }else if(data== "exist"){
  13. $(".errorFont").text("用户名已注册2");
  14. $(".errorFont").css("color","green");
  15. }else if(data == "noexist"){
  16. $(".errorFont").text("用户名未注册2");
  17. $(".errorFont").css("color","red");
  18. }
  19. },
  20. error:function(data){
  21. $(".errorFont").text("发生未知错误,请联系管理员2!");
  22. $(".errorFont").css("color","red");
  23. }
  24. });
  25. });

后端逻辑处理:

  1. //验证用户是否存在,返回一个转成json字符串的数据
  2. @RequestMapping("/nameProving2")
  3. @ResponseBody
  4. public String nameProving2(String name){
  5. String result="";
  6. if(StringUtils.isEmpty(name)){
  7. result= "empty";
  8. }else{
  9. Userss user=userService.getUserByName(name);
  10. if(user!=null)
  11. result= "exist";
  12. else
  13. result= "noexist";
  14. }
  15. return JSONArray.toJSONString(result);
  16. }

3、返回一个字符串

前端js请求:

重点:datatype:"text"

  1. //返回值为String的形式,dataType:"text"
  2. $(".name").blur(function(){
  3. $.ajax({
  4. type:"Post",//请求类型
  5. url:"/mvc-demo/user/nameProving3?t="+new Date().getTime(),//请求的url
  6. data:{name:$(".name").val()},//请求参数
  7. dataType:"text",//ajax接口(请求url)返回的数据类型
  8. success:function(data){//data:返回数据(json对象)
  9. if(data == ""empty""){//name为空,错误提示
  10. $(".errorFont").text("用户名为不能为空3!");
  11. $(".errorFont").css("color","red");
  12. }else if(data== ""exist""){
  13. $(".errorFont").text("用户名已注册3");
  14. $(".errorFont").css("color","green");
  15. }else if(data == ""noexist""){
  16. $(".errorFont").text("用户名未注册3");
  17. $(".errorFont").css("color","red");
  18. }
  19. },
  20. error:function(data){
  21. $(".errorFont").text("发生未知错误,请联系管理员3!");
  22. $(".errorFont").css("color","red");
  23. }
  24. });
  25. });

后端逻辑处理:

  1. //验证用户是否存在,返回一个字符串的数据
  2. /**
  3. * 此方法中有多个注解 method:指定请求方式
  4. * required:表示参数是否必须 默认为必须 fslse为不是必须
  5. * @param name
  6. * @return
  7. */
  8. @RequestMapping(value="/nameProving3",method=RequestMethod.POST,params="name")
  9. @ResponseBody
  10. public String nameProving3(@RequestParam(value="name",required=false)String name){
  11. String result="";
  12. if(StringUtils.isEmpty(name)){
  13. result= "empty";
  14. }else{
  15. Userss user=userService.getUserByName(name);
  16. if(user!=null)
  17. result= "exist";
  18. else
  19. result= "noexist";
  20. }
  21. return result;
  22. }

4、返回一个类对象的数据

前端js请求:

  1. //返回值为类对象的形式,dataType:"json"
  2. $(".name").blur(function(){
  3. $.ajax({
  4. type:"Post",//请求类型
  5. url:"/mvc-demo/user/nameProving4?t="+new Date().getTime(),//请求的url
  6. data:{name:$(".name").val()},//请求参数
  7. dataType:"json",//ajax接口(请求url)返回的数据类型
  8. success:function(data){//data:返回数据(json对象)
  9. if(data == null){//name为空,错误提示
  10. $(".errorFont").text("用户名为空或者用户名不存在4!");
  11. $(".errorFont").css("color","red");
  12. }else{
  13. $(".errorFont").text("用户名"+data.name+"已注册4");
  14. $(".errorFont").css("color","green");
  15. }
  16. },
  17. error:function(data){
  18. $(".errorFont").text("发生未知错误,请联系管理员2!");
  19. $(".errorFont").css("color","red");
  20. }
  21. });
  22. });

后端逻辑处理:

  1. //返回一个类对象
  2. @RequestMapping("/nameProving4")
  3. @ResponseBody
  4. public Userss nameProving4(String name){
  5. System.out.println("进来了4");
  6. Userss user=userService.getUserByName(name);
  7. if(user==null){
  8. return null;
  9. }
  10. return user;
  11. }
原文地址:https://www.cnblogs.com/zhuyeshen/p/11419522.html