controller向ajax传值问题+spring MVC

本文介绍了如何在Spring MVC中使用@RequestBody和@ResponseBody注解将控制器的返回值直接写入HTTP响应体,以便于通过Ajax异步获取JSON数据。示例展示了Controller如何通过Map向Ajax传递`triggerList`和`size`两个参数,以及前端如何使用jQuery遍历并处理接收到的数据。

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

@responsebody表示该方法的返回结果直接写入HTTP response body
一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。比如异步获取json数据,加上@responsebody后,会直接返回json数据。

控制层通过map向前台传递数据   如:triggerListsize两个

@RequestMapping(value="/host/getTrigger", method = RequestMethod.POST )

@ResponseBody
  public  Map<String, Object>  getTrigger(Integer templateId , Integer hostId) {
Map<String, Object> modelMap = new HashMap<String, Object>();
System.out.println(templateId+"           "+hostId);
List<Integer> general = new ArrayList<Integer>(); //查询hostid=?下的trigger数据所用的valea=1triggerids
List<Integer> temp = new ArrayList<Integer>();
List<Integer> hostid = new ArrayList<Integer>();
general.add(1);
temp.add(templateId);
hostid.add(hostId);
   List<TriggerObject> triggerList = triggerService.findTriggerByhostortemplateid(hostid, temp, general);
   modelMap.put("triggerList", triggerList);
   modelMap.put("size", triggerList.size());
return modelMap; 
    

    } 

前台通过data.map的关键字  data.triggerListdata.size都可以,其中item是取list中每个对象属性的值,ilist每个对象的下表。(鄙人这样粗浅的理解,便于自己记忆,如果有理解不了的可以百度在细细看看。)

success : function(data) {

  var size = data.size;
  $.each(data.triggerList, function(i, item) {  
  if(item.value==1||item.priority>=3){

自己的逻辑块。。。

}

};

}

这样也可以:

 

$.ajax({

url:"check",

type:"post",

data:params,//要传递的数据

dataType:"json",

success:function(m){

var list=m.gslist;      / /gslistmap的键

if($("#goodName").html("")!=null){

$("#goodName").html("");

$("#goodName").append("<option>请选择商品</option>") ;

}

for(var i=0;i<list.length;i++){

 

$("#goodName").append("<option value="+list[i].goods.goodsId+">"+list[i].goods.goodsName+"</option>");

}

},

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值