How to pass data to the background in the foreground script via JSON (with Ajax from Microsoft) _ Practical Tips

Source: Internet
Author: User

First, we'll introduce a JSON script in the foreground to facilitate the serialization of 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

Copy Code code 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 ("Save Success");
Parent.$.fancybox.close ();
}
Else
{
alert (result);
}
}

Then we are in the background, deserialization, and using the value. Because we use Ajax, we have to add [Ajax.ajaxmethod] features to the backend method and add Ajax to the CS of your front page. Look at the specific use

How to use Microsoft Ajax Library (Ajax.ajaxmethod) http://www.jb51.net/article/40764.htm

Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax (typeof (Youjiao.xxt.BLL.Controller.AjaxController));
if (! IsPostBack)
{
Databind ();
}
}

Copy Code code 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 = "Session is invalid, please login again!" ";
}
}
catch (Exception ex)
{
result = ex. message;
}
return result;
}

Image:

Copy Code code 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;}
}

This can be very good to avoid in the foreground JS inside a large number of parameters, the background can be directly deserialized to the class to point out the value of the member

Related Article

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.