前台给后台传JSON字符串,后台解析并保存

前台

 function BMSure() {
var DanWeiName = $("[id$='BusinessName']").val();
var Address = $("[id$='Address']").val();
if ((DanWeiName != "") && (Address != "")) {
var planGuid = $("[id$='hid_planGuid']").val();
//var displayname = $("[id$='hid_displayName']").val();
var num = $("[id$='hid_sum']").val();
$.ajax({
type: "POST",
contentType: "application/json;charset=utf-8", url: "../Ashx/SaveBMInfo.ashx?PlanGuid=" + planGuid,
data: JSON.stringify(GetJsonData()),
dataType: "json",
success: function (message) {
if (message == ) { ConfirmBM();
//ReturnLogin(DanWeiName); }
},
error: function (message) {
if (message == ) {
layer.msg("保存失败");
}
}
})
}
else {
layer.msg("请将单位信息填写完整");
return false;
}
}; function ReturnLogin(DanWeiName) {//返回登录名密码
layer.open({
type: ,
title: "提示信息",
area: ['400px', '220px'],
content: ['LoginID_PassWord.aspx?DanWeiName=' + DanWeiName],
skin: 'layer-ext-iframe',
//关闭按钮刷新父页面
cancel: function () {
window.location.reload();
}
});
} function ConfirmBM() {
//是否继续报名
layer.confirm('报名成功,是否继续报名?',
{
skin: 'layui-layer-molv', btn: ['继续报名', '关闭'] //按钮
}, function (index) {
layer.close(index);
window.location.reload();
}, function (index) {
layer.close(index); var index2 = parent.layer.getFrameIndex(window.name);
parent.layer.close(index2);//关闭弹出层
}); } function GetJsonData() {
var temp = $("[id$='hid_sum'").val();//添加人员的数量
var json_person = '';
for (var i = ; i <= temp; i++) {
var text = "";
$("[id$='addPerson" + i + "']").find("td").children("input").each(function () {
text += $(this).val() + ",";
})
var a = new Array();
a = text.split(',');
if (a != "" && a != null) {
var json_persontemp = {
"Name": a[],
"Sex": a[],
"Identity": a[],
"Brithday": a[],
"Phone": a[]
}
}
if (i == temp)
json_person += JSON.stringify(json_persontemp);
else
json_person += JSON.stringify(json_persontemp) + ','; }; var persontemp = eval('[' + json_person + ']'); var json = {
"DanWeiName": $("[id$='BusinessName']").val(),
"DetailAddress": $("[id$='Address']").val(),
"BelongFenJu": $("[id$='BelongFenJu']").val(),
"PersonData":
//JSON.parse(json_person)
persontemp }; return json;
}

后台(此处 使用SaveBMInfo 一般处理程序,后台直接写也一样)

  public class SaveBMInfo : IHttpHandler
{
Epoint.KSPXBase.Bizlogic.BLL.B_PK_Unit b_unit = new Epoint.KSPXBase.Bizlogic.BLL.B_PK_Unit();
Epoint.KSPXBase.Bizlogic.BLL.B_PK_User b_user = new Epoint.KSPXBase.Bizlogic.BLL.B_PK_User();
Epoint.PeiXun.Bizlogic.BLL.PXBaoM.B_PX_BaoM b_baom = new Epoint.PeiXun.Bizlogic.BLL.PXBaoM.B_PX_BaoM();
M_PK_Unit m_unit = new M_PK_Unit();
M_PK_User m_user = new M_PK_User();
M_PX_BaoM m_baom = new M_PX_BaoM(); List<PersonDataJSON> list_PersonData = new List<PersonDataJSON>();
public void ProcessRequest(HttpContext context)
{
string result = "";
context.Response.ContentType = "application/json";
string PlanGuid = context.Request.QueryString["PlanGuid"].ToString(); var data = context.Request;
var sr = new StreamReader(data.InputStream);
var stream = sr.ReadToEnd();
var javaScriptSerializer = new JavaScriptSerializer();
var PostedData = javaScriptSerializer.Deserialize<RequestDataJSON>(stream); try
{
#region
if (PostedData != null)
{
m_unit.RowGuid = Guid.NewGuid().ToString();//单位唯一标识
m_unit.DanWeiName = PostedData.DanWeiName;//单位名称
m_unit.DetailAddress = PostedData.DetailAddress;//单位地址
m_unit.BelongFenJu = PostedData.BelongFenJu;//所属* --报名点
m_unit.LoginID = ConvertToPY(PostedData.DanWeiName);//登录名
m_unit.Password = common.authPassword("");//密码
m_unit.AddType = "";//报名方式 1-前台报名 2-后台添加 3-接口获取
m_unit.UserType = "";//会员类别 0-个人 1-企业 2-学校 3-培训机构
m_unit.IsEnable = "";
m_unit.DelFlag = "";
m_unit.Status = "";//会员状态
b_unit.Insert(m_unit); var peopleInfo = PostedData.PersonData;
for (int i = ; i < peopleInfo.Count; i++)
{
m_user.RowGuid = Guid.NewGuid().ToString();//人员唯一标识
m_user.LoginID = PostedData.PersonData[i].Identity;//登录名为身份证号码
m_user.Password = common.authPassword("");//密码
m_user.DanWeiGuid = m_unit.RowGuid;//单位Guid
m_user.DanWeiName = m_unit.DanWeiName;//单位名称
m_user.Name = PostedData.PersonData[i].Name;//人员姓名
if (PostedData.PersonData[i].Sex == "男")//性别
m_user.Sex = "";
else
m_user.Sex = "";
m_user.IdentityNum = PostedData.PersonData[i].Identity;//身份证号
m_user.BirthDay =Convert.ToDateTime(PostedData.PersonData[i].Brithday);//出生日期
m_user.MobilePhone = PostedData.PersonData[i].Phone;//手机号
m_user.AddType = "";//帐号添加方式 1-注册 2-企业添加 3-后台添加
m_user.UserType = "";//会员类别 0-个人 1-企业 2-学校 3-培训机构
m_user.IsEnable = "";
m_user.DelFlag = "";
m_user.Status = "";//会员状态
m_user.IdentityType = "";//身份证
b_user.Insert(m_user); m_baom.RowGuid = Guid.NewGuid().ToString();//报名唯一标识
m_baom.BMType = "";//报名类别
m_baom.AddDate = m_baom.OperateDate = DateTime.Now;//报名日期
m_baom.Name = m_user.Name;//姓名
m_baom.LoginID = m_user.LoginID;//登录名
m_baom.UserGuid = m_user.RowGuid;//人员Guid
m_baom.PlanGuid = PlanGuid;//计划Guid
m_baom.DanWeiGuid = m_unit.RowGuid;//单位Guid
m_baom.DanWeiName = m_unit.DanWeiName;//单位名称
m_baom.BaoMDNum = m_unit.BelongFenJu;//报名点 --所属*
b_baom.Insert(m_baom);
}
}
#endregion
result = "";
}
catch (Exception ex)
{
result = "";
}
context.Response.ContentType = "text/plain";
context.Response.Write(result);
} public bool IsReusable
{
get
{
return false;
}
} public string ConvertToPY(string Text)
{
char[] chText = Text.ToCharArray();
string pyText = "";
foreach (char ch in chText)
{
pyText += new ChineseChar(ch).Pinyins[].Substring(, );
}
return pyText;
} public class RequestDataJSON
{
public string DanWeiName { get; set; }
public string DetailAddress { get; set; }
public string BelongFenJu { get; set; }
public List<PersonDataJSON> PersonData; }
public class PersonDataJSON
{//人员
public string Name { get; set; }
public string Sex { get; set; }
public string Identity { get; set; }
public string Brithday { get; set; }
public string Phone { get; set; }
}
}
上一篇:在SpringMVC Controller中注入Request成员域


下一篇:【Spring学习】SpringMVC demo搭建