How to transmit data to the background through json in the foreground script (using ajax provided by Microsoft)

Source: Internet
Author: User

First, we need to introduce the json script at the front end to facilitate js Object serialization.

<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 serialize it finally.

Copy codeThe Code is as follows:
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 ("saved successfully ");
Parent. $. fancybox. close ();
}
Else
{
Alert (Result );
}
}

Then we perform deserialization in the background and use the value. because we use ajax, we need to add [Ajax. ajaxMethod] feature, and add Ajax registration to the cs on the page where your front-end is located. for more information, see

The use of Microsoft ajax Library (ajax. ajaxMethod) http://www.jb51.net/article/40764.htm

Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
Ajax. Utility. RegisterTypeForAjax (typeof (Youjiao. xxt. BLL. Controller. AjaxController ));
If (! IsPostBack)
{
Databind ();
}
}

Copy codeThe Code is as follows:
[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 log on again! ";
}
}
Catch (Exception ex)
{
Result = ex. Message;
}
Return Result;
}

Image:

Copy codeThe Code is as follows:
[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 ;}
}

In this way, you can avoid passing in a large number of parameters in front-end js, And the backend can also be directly deserialized into a class to point out the Member values.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.