How to use Microsoft's own ajax in json to transfer data from the foreground script to the background of

  • 2020-06-23 00:11:56
  • OfStack

First, we'll introduce the json script in the foreground to serialize the js object

< script type="text/javascript" src="/js/jquery.json-2.4.min.js" > < /script >

Then we declare a class in the foreground, put the value you want to save into the class, and finally serialize it


function Save() {
            var examId = '<%=ExamId %>';
            var yearTerm = $("#<%=DDLYearTerm.ClientID %>").val();
            var examType = $("#<%=DDLExamType.ClientID %>").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(" Save success ");
                parent.$.fancybox.close();
            }
            else
            {
                alert(Result);
            }
        }

Then in the background, we deserialize and use values. Because we use ajax, we add the [Ajax.AjaxMethod] feature to the background method, and add Ajax registration to the cs page on your foreground page

The use of the Microsoft ajax library (ajax ajaxMethod) https: / / www ofstack. com 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<EditExam>(value);
                    ExamController eController = new ExamController();
                    eController.EditExam(editExam);
                    Result = "Success";
                }
                else
                {
                    Result = " The session is invalid, please login again! ";
                }
            }
            catch (Exception ex)
            {
                Result = ex.Message;
            }
            return Result;
        }

Image:


[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; }
    }

This is a good way to avoid passing in too many parameters in foreground js, and the background can be deserialized directly into the class to point out the values of the members


Related articles: