版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/qq_36910987/article/details/84789356
ajax向后台传递数组
List = { a: [{“x” : x,“y” : y}], b: [{“n” : n,“m” : m}]};
下面直接贴代码;
1 页面.
//下面我用到了layer.js。layer页面框架,我感觉很方便很简单实用,百度搜索layer就可以看到了
// 定义数组
List = { a: [], b: [] };
//这里的x什么的.... 我没传值只是举例子 qaq
List.a.push({"x" : x},{"y" : y});
List.b.push({"n" : n},{"m" : m});
//这里给出如何循环的方法
List.a.forEach(function(i, index, a){
console.log(i.x);//这就是值啦。
});
//下面ajax传递
var loadindex;
$.ajax({
url : get_web_url()+"/exam/SaveAnswer.action",
type : 'POST',
async:false,//这里我设置成同步
contentType: "application/json; charset=utf-8",
data: JSON.stringify(List),
success : function(data) {
if(data=='no'){
layeropen("这里写提示信息!","这里写要跳转的地址,你自己决定啦!");
}else{
layeropen("这里写提示信息!","这里写要跳转的地址,你自己决定啦!");
}
},
beforeSend: function() {
loadindex = parent.layer.msg('正在请求请稍等...',{icon: 16,time :9999999,shade: [0.4, '#000']});
},
error : function() {
parent.layer.msg('系统异常', {icon: 2});
}
});
//这个是获取当前项目的一些地址用的
//例如 http://localhost:8080/projectname/index.action
var strFullPath=window.document.location.href; //获取当前浏览器访问地址 http://localhost:8080/projectname/index.action
var strPath=window.document.location.pathname; //获取去除域名的后面的访问地址 /projectname/index.action
var pos=strFullPath.indexOf(strPath);
var prePath=strFullPath.substring(0,pos);//获取去除域名的后面的访问地址 http://localhost:8080
var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);//获取去除域名的后面的访问地址/progectname
var webPath = prePath+postPath;// http://localhost:8080/projectname
function get_web_url() {
return webPath;
}
//这个是layer的enmmm提示框?!
function layeropen(msg,url){
var opens1 = layer.open({
tips: 1,
title: '提示信息', //显示标题
content: msg,
maxWidth:'280px',
btn: ['确认'],
time: 5000, //2秒关闭(如果不配置,默认是3秒)
closeBtn: 0, //不显示关闭按钮
yes: function(index, layero) {
layer.close(opens1);
if(url!=null){
var index1 = parent.layer.getFrameIndex(window.name); //获取当前窗体索引
parent.layer.close(index1); //执行关闭
location.href = get_web_url()+url;
//这里也可以直接刷新父页面,这样的话上面就不用传地址了,url的判断也不需要喽!
//window.parent.location.reload();
}
},
end: function(index, layero) {
layer.close(opens1);
if(url!=null){
var index1 = parent.layer.getFrameIndex(window.name); //获取当前窗体索引
parent.layer.close(index1); //执行关闭
location.href = get_web_url()+url;
//这里也可以直接刷新父页面,这样的话上面就不用传地址了,url的判断也不需要喽!
//window.parent.location.reload();
}
}
});
}
1 java代码.
@Controller层
/**
* 保存
* @param List
* @param model
* @return
*/
@ResponseBody
@RequestMapping(value = "/SaveAnswer",method=RequestMethod.POST)
public String SaveAnswer(@RequestBody String List, Model model) {
String map = examService.SaveAnswer(QList);
return map;
}
@Service层
//enmmm 不写了!Service层没啥写的
@ServiceImpl层
//这个是json的导包
import com.alibaba.fastjson.JSONObject;
import net.sf.json.JSONArray;
@Override
public String SaveAnswer(String List) {
JSONObject jo=new JSONObject();
//如果页面传的是json字符串,用下列方式解析
Map<String, Object> map = (Map<String, Object> )jo.parse(List); //string转map
/**修改题目/添加选项(已存在的)*/
List<Map> a = JSONArray.fromObject(map.get("a"));//题目
List<Map> b = JSONArray.fromObject(map.get("b"));//选项号
//循环取值
for(Map l :a) {
System.out.println(l.get("x"));
}
updateAnswer(a);
return "yes";
}
//我是直接存到数据库的,用jdbcTemplate的批量处理、下面只是例子啦!
//我用的是MySql数据库
public void updateAnswer(final List<Map> a){
String sql="INSERT INTO 表1 (字段1,字段2) VALUES (?,?)";
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public int getBatchSize() {
return a.size();
}
@Override
public void setValues(PreparedStatement ps, int i)throws SQLException {
ps.setObject(1, a.get(i).get("x"));
ps.setObject(2, a.get(i).get("y"));//未做对
}
});
}
好啦!就写到这了哈!!!