Jquery uses ajax to call backend method instances

Source: Internet
Author: User

Copy codeThe Code is as follows:
Syntax:
$ (Function (){
Var callback = function (r) {// r indicates the data returned by the background data.
}
$. Get ("", callback); // path of the write method in quotation marks
});

In this way, the data obtained by the background method is displayed on the html front-end.


The following is a js and background method written at work:

Js:
Copy codeThe Code is as follows:
$ (Function (){
Var callback = function (r ){
Var data = "<marquee width = '2017; 'height = '250px; 'rollamount = '2' scrolldelay = '4' ction = 'up' behavior = 'loe' onmouseout = 'this. start () 'onmouseover = 'this. stop () '> <ul>"
Var data2 = "<div class = 'marquee _ div '> <span onclick = 'closelist () '> </span> discount list </div> ";
For (var I = 0; I <r. length; I ++ ){
Data + = ("<li> <label>" + r [I]. time + "</label> <span>" + r [I]. customerName + "</span> if you have successfully applied for a trial, you will receive a discount on the annual service fee. Congratulations! </Li> ");

}
Data + = "</marquee> </ul> ";
$ ("# 01_companys" pai.html (data );
Var t_data2 = "";
For (var I = 0; I <r. length; I ++ ){
If (I % 2 = 0 ){
T_data2 + = "</div> <div class = 'listtitle'> ";
}
T_data2 + = "<div style = 'width: 49%; height: 40px; line-height: 40px; float: left'>" + (I + 1) + ". "+ r [r. length-I-1]. customerName + "</div> ";
}

$ ("# 01_list" ).html (data2 + (t_data2.length> 0? T_data2.substr (6, t_data2.length-6): "") + "</div> ");
}
$. Get ("/activity/trycompany", callback );
});

Background code (mvc4 ):

Copy codeThe Code is as follows:
Public ActionResult TryCompany ()
{
String CustomerType = Res. CustomerType. TestCustomer. ToString ();
IList <Customers> vlist = o_Res.GetHtmlCustomers (1,100, out pCount, (a => a. CustomerType = CustomerType ));

Var list = vlist. ToList (). ConvertAll (s =>
{
Return new {s. CustomerName, time = s. AddDate. HasValue? S. AddDate. Value. ToString ("MM-dd HH: mm "):""};
});
Return Json (list, JsonRequestBehavior. AllowGet );
}

In the background, a list, jq call, traversal, and display on the front end.

For a simple example, there must be a simpler method to mine in the future.

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.