如何将一个数组通过ajax传递到后台控制器中,后台代码怎么写
{//获取选中行varrows=$("#ExchangGift2").datagrid('getChecked');前台:varids=[];$.each(rows,fu...
{
//获取选中行
var rows = $("#ExchangGift2").datagrid('getChecked');
前台: var ids = [];
$.each(rows, function (index, row) {
ids.push(row.ExchangGift.EG_ID,row.ExchangLog.EL_Number);
});
$.get('/ExchangLog/DuiHuan', { ids: ids },
function (data)
{
if (data == "1") {
alert('兑换成功!');
}
else
{
alert('兑换失败');
}
}
);
后台: public ActionResult DuiHuan(string[] ids)
{
return View();
} 展开
//获取选中行
var rows = $("#ExchangGift2").datagrid('getChecked');
前台: var ids = [];
$.each(rows, function (index, row) {
ids.push(row.ExchangGift.EG_ID,row.ExchangLog.EL_Number);
});
$.get('/ExchangLog/DuiHuan', { ids: ids },
function (data)
{
if (data == "1") {
alert('兑换成功!');
}
else
{
alert('兑换失败');
}
}
);
后台: public ActionResult DuiHuan(string[] ids)
{
return View();
} 展开
1个回答
展开全部
1、前台jsp中数组变量: var checkedData = [];//前台变量1 2、在ajax中设置该属性: traditional: true,//传数组进后台需要设置该属性1 Ajax代码示例: $.ajax({ url : URL_Business_QFCJPage, data : { "checkedData" : checkedData }, dataType : 'json', type : "post", traditional: true,//属性在这里设置 success : function(message) { //回调函数代码省略 }, waitMsg : '正在处理数据....' });12345678910111213 3、后台Servlet中这样获取: String[] s = request.getParameterValues("checkedData");//获取数组使用该函数1 Servlet代码示例: package com.***; @Controller public class QFCJPageController { @RequestMapping("/business/QFCJPage") @ResponseBody public void CJ(HttpServletRequest request, HttpServletResponse response) throws IOException { //如果数组有中文,注意设置编码
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询