Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method steps of transferring data to the background through json in the foreground script

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

本篇内容介绍了"在前台脚本通过json传递数据到后台的方法步骤"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

首先,我们要在前台引入json的脚本,以便于把js对象序列化

然后我们在前台声明一个类,将你要保存的值放到类里面,最后序列化

复制代码 代码如下:

function Save() {

var examId = '';

var yearTerm = $("#").val();

var examType = $("#").val();

var examDate = $("#ExamDate").val();

var examName = $("#ExamName").val();

var exam = {};

exam["ExamId"] = examId;

exam["YearTerm"] = yearTerm;

exam["ExamType"] = examType;

exam["ExamDate"] = examDate;

exam["ExamName"] = examName;

var json = $.toJSON(exam);

var Result = AjaxController.EditExam(json).value;

if (Result == "Success")

{

alert("保存成功");

parent.$.fancybox.close();

}

else

{

alert(Result);

}

}

然后我们在后台,进行反序列化,并使用值.因为我们使用ajax所以要在后台的方法上加上[Ajax.AjaxMethod]特性,并且在你前台所在页面的cs里面也要加上Ajax的注册.具体使用看

微软 ajax 库 的使用方法( ajax.ajaxMethod) https://www.jb51.net/article/40764.htm

复制代码 代码如下:

protected void Page_Load(object sender, EventArgs e)

{

Ajax.Utility.RegisterTypeForAjax(typeof(Youjiao.xxt.BLL.Controller.AjaxController));

if (!IsPostBack)

{

Databind();

}

}

复制代码 代码如下:

[Ajax.AjaxMethod]

public string EditExam(string value)

{

string Result = "";

try

{

if (HttpContext.Current.Request.IsAuthenticated)

{

EditExam editExam = JsonSerializeHelper.DeserializeFromJson(value);

ExamController eController = new ExamController();

eController.EditExam(editExam);

Result = "Success";

}

else

{

Result = "会话无效,请重登录!";

}

}

catch (Exception ex)

{

Result = ex.Message;

}

return Result;

}

复制代码 代码如下:

[Serializable]

public class EditExam

{

public string ExamId { get; set; }

public string YearTerm { get; set; }

public string ExamType { get; set; }

public string ExamDate { get; set; }

public string ExamName { get; set; }

}

"在前台脚本通过json传递数据到后台的方法步骤"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report