zoukankan      html  css  js  c++  java
  • Spring MVC 中 AJAX请求并返回JSON

    一、以ModelAndView的方式返回

    先看下JavaScript代码:

     1 /**
     2  * 保存-同步(版本控制库)
     3  */
     4 function saveSynchronizedVcHorse(obj) {
     5     var ss = $("#SynchronizedSelection div");
     6     var cacheSelectAry = new Array()
     7     for(var i = 0; i < ss.length; i ++) {
     8         //alert(ss.eq(i).html());
     9         //alert(ss.eq(i).find('label').html());
    10         var o=ss.eq(i).find('label').find("input[type='checkbox']");
    11         
    12         var s = $(o).attr('checked');//由于$(obj).attr("checked", false); checked属性会被
    13         if(s != null) {
    14             cacheSelectAry.push($(o).attr('value'));
    15         } 
    16     }
    17     if(cacheSelectAry.length == 0) {
    18         alert("请选择城市");
    19         return;
    20     }
    21     var json = "SynchronizedBean={"exceptionId":""+exceptionId+"","cityIds":["
    22     for(var i = 0; i < cacheSelectAry.length; i ++) {
    23         if(i == cacheSelectAry.length -1) {
    24             json = json +"""+ cacheSelectAry[i] + """;
    25         } else {
    26             json = json + """ + cacheSelectAry[i] + "",";
    27         }
    28     }
    29     json = json + "]}";
    30     $.ajax({
    31            type: "POST",
    32            dataType: "json",
    33            url: "../main/saveSynchronizedData",
    34            data: json,
    35            success: function(msg){
    36                alert(msg.main);
    37            },
    38            error: function () {//XMLHttpRequest, textStatus, errorThrown
    39                alert("请求失败"); 
    40            } 
    41     });
    42 }

    主要是看ajax请求部分。

    再看Spring 中控制器的代码:

     1 @RequestMapping(value = "/saveSynchronizedData", method = RequestMethod.POST)
     2     public @ResponseBody ModelAndView saveSynchronizedData(@RequestParam("SynchronizedBean") String mSynchronizedJSON) {
     3         ObjectMapper objectMapper = new ObjectMapper();
     4         Map<String, String> map = new HashMap<String, String>();
     5         try {
     6             SynchronizedBean bean = objectMapper.readValue(mSynchronizedJSON, SynchronizedBean.class);
     7             if(bean != null) {
     8                 String[] ary = bean.getCityIds();
     9                 if(ary != null && ary.length > 0) {
    10                     for(String s : ary) {
    11                         VCHousePo po = new VCHousePo();
    12                         po.setExceptionId(bean.getExceptionId());
    13                         po.setCustomerCode(s);
    14                         po.setCreateTime(new Date());
    15                         po.setExceptionState(0);
    16                         vcHService.add(po);
    17                     }
    18                 }
    19             }
    20             map.put("msg", "success");
    21         } catch (JsonParseException e) {
    22             e.printStackTrace();
    23             map.put("msg", "error");
    24         } catch (JsonMappingException e) {
    25             e.printStackTrace();
    26             map.put("msg", "error");
    27         } catch (IOException e) {
    28             e.printStackTrace();
    29             map.put("msg", "error");
    30         }
    31         return new ModelAndView(new MappingJackson2JsonView(),map);
    32     }

    这里是通过ModelAndView的方式进行返回JSON的。另外:MappingJackson2JsonView使用的package是 

    import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

    二、返回String的形式

    这种方式不做实验,请查看http://hw1287789687.iteye.com/blog/2128304

  • 相关阅读:
    spring-data-elasticsearch (elasticsearch 6.7.0) @Document 和 @Field 注解详解
    干货链接(大神)
    Excel word PDF导入导出 Easy POI
    helm 源大集合
    K8s学习干货
    K8S
    阳明大神---容器时代
    K8s高可用集群部署
    最小 docker_lpnm系统
    python 协程和异步编程全解
  • 原文地址:https://www.cnblogs.com/royi123/p/5239431.html
Copyright © 2011-2022 走看看