$.ajax{
url:"xxxx",
data:{
array: ["Jason","Sean","Danny"]
}
}
如果单纯写成这样,在java后台是无法取到参数的,因为jQuery需要调用jQuery.param序列化参数,
jQuery.param( obj, traditional ),默认的话,traditional为false,即jquery会深度序列化
参数对象,以适应如PHP和Ruby on Rails框架, 但servelt api无法处理,我们可以设置traditional
为true阻止深度序列化,然后序列化结果如:
array: ["Jason", "Sean", "Danny"]=>array=Jason&array=Sean&array=Danny。
这样,我们就可以在后台通过request.getParameterValues()来获取参数的值数组了。
代码:$.ajax({
url:url,
async:false,
type:"POST",
data:data,
traditional:true,
dataType:"json",
success:function(result){
if(result.status == '0'){
setTimeout(function(){
layer.msg('删除失败', {icon: 1})
}, 500 );
return false;
}else{
window.location.reload();
return true;
}
}
});
java后台将数据传给页面
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String[] data = req.getParameterValues("array");
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < data.length; i++) {
list.add(data[i]);
System.out.println(data[i]);
}
//将list装换为Json数组(JSONArray)
String json = JSONArray.fromObject(list).toString();
resp.setHeader("Cache-Control", "no-cache");
resp.setContentType("text/json; charset=utf-8");
resp.getWriter().print(json);
resp.getWriter().flush();
}
service层:map.put("lidArr",lidArr);
<select id="queryLdunm" parameterType="hashmap" resultType="hashmap">
SELECT lid ,count(*) as ldum FROM T_COMMON_LAWC LAWC WHERE 1=1 and flcid IS NOT NULL and LID IN
<foreach collection="lidArr" open="(" separator="," close=")" item="law">
#{law}
</foreach>
group by lid
</select>
js将map,数组等对象传送给后台
最新推荐文章于 2024-06-24 17:24:10 发布