您的位置:首页 > 其它

ajax传递复杂参数

2015-12-21 13:54 246 查看
使用mvc测试Demo

================view================
@{
ViewBag.Title = "Index";
Layout = null;
}
<h2>ajax请求参数测试</h2>

<script src="~/Scripts/jquery-1.8.2.js"></script>
<div>
<div id="div_Data">
<form id="_form">

<table>
<tbody>
<tr>
<td><label>编号:</label></td>
<td><input type="text" name="ID" value="2" /></td>
</tr>
<tr>
<td><label>名称:</label></td>
<td><input type="text" name="Name" value="测试人员" /></td>
</tr>
<tr>
<td><label>列表:</label></td>
<td><input type="text" name="ListStr" value="测试A,测试B" /></td>
</tr>
<tr>
<td><label>日期:</label></td>
<td><input type="text" name="Date" value="2015-12-17 10:50:30" /></td>
</tr>
</tbody>
</table>

</form>
</div>
<input id="inpBtn" type="button" value="点击事件+get" />
<input id="inpBtn2" type="button" value="点击事件2+get" />
<input id="inpBtn3" type="button" value="点击事件3+post" />
<input id="inpBtn4" type="button" value="点击事件4+post" />

</div>
<script type="text/javascript">
Array.prototype.toJson = function () {
var json = {};
for (var i in this) {
var o = this[i];
if (o.name != '__VIEWSTATE') {
if (typeof (json[o.name]) == 'undefined')
json[o.name] = o.value;
else
json[o.name] += "," + o.value;
}
}
return json;
}
$('#inpBtn').click(function () {
debugger;
var _formObj = $('#_form');
var arr = _formObj.serializeArray();//获取表单数据
for (var i = 0, l = arr.length; i < l;i++){
if (arr[i].name == "ListStr") {
var list = arr[i].value.split(',');
delete arr[i];
for (var j = 0, len = list.length; j < len; j++) {
var item = {
name: "ListStr",
value: list[j]
};
arr.push(item);
}
break;
}
}
$.ajax({
url: '/AjaxRequest/GetTestAjax',
type: 'get',
data: arr,
async: false,
success: function (data, status) {
debugger;
},
error: function (ex) {
debugger;
}
});
});

$('#inpBtn2').click(function () {
debugger;
var _formObj = $('#_form');
var arr = _formObj.serializeArray();
var jsonObj = arr.toJson();
jsonObj.ListStr = jsonObj.ListStr.split(',');
var json = JSON.stringify(jsonObj);//object转字符串
$.ajax({
url: '/AjaxRequest/GetTestAjax2',
type: 'get',
data: { 'json': json },
async: false,
success: function (data) {
debugger;
},
error: function (ex) {
debugger;
}
});

});
$('#inpBtn3').click(function () {
debugger;
var _formObj = $('#_form');
var arr = _formObj.serializeArray();
var jsonObj = arr.toJson();
jsonObj.ListStr = jsonObj.ListStr.split(',');
var aaa = jsonObj.ListStr;
//var list = JSON.stringify(aaa);
//var json = JSON.stringify(jsonObj);//object转字符串
var para = {
test: jsonObj,
list: aaa
};
var paraStr = JSON.stringify(para);
$.ajax({
url: '/AjaxRequest/GetTestAjax3',
type: 'post',
contentType: "application/json",
data: paraStr,
async: false,
success: function (data) {
debugger;
},
error: function (ex) {
debugger;
}
});
});
$('#inpBtn4').click(function () {
debugger;
var _formObj = $('#_form');
var arr = _formObj.serializeArray();
var jsonObj = arr.toJson();
jsonObj.ListStr = jsonObj.ListStr.split(',');
var json = JSON.stringify(jsonObj);//object转字符串
$.ajax({
url: '/AjaxRequest/GetTestAjax4',
type: 'post',
contentType: "application/json",
data: json,
async: false,
success: function (data) {
debugger;
},
error: function (ex) {
debugger;
}
});
});
</script>
===============controller===============
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
namespace ContentReplace.Controllers
{
public class AjaxRequestController : Controller
{
//
// GET: /AjaxRequest/
public ActionResult Index()
{
return View();
}
public JsonResult GetTestAjax(TestClass test)
{
var data = "";
return Json(data, JsonRequestBehavior.AllowGet);
}
public JsonResult GetTestAjax2()
{
var json = Request["json"];
var jss = new JavaScriptSerializer();
var arr = jss.Deserialize<TestClass>(json);
var data = "";
return Json(data, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public JsonResult GetTestAjax3(TestClass test,List<string> list)
{
var data = "";
return Json(data, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public JsonResult GetTestAjax4(TestClass test)
{
var data = "";
return Json(data, JsonRequestBehavior.AllowGet);
}
}
public class TestClass
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime Date { get; set; }
public List<string> ListStr { get; set; }
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ajax 传参