Home >Web Front-end >JS Tutorial >jquery uses ajax to call background method instance_jquery

jquery uses ajax to call background method instance_jquery

WBOY
WBOYOriginal
2016-05-16 17:24:38902browse

Copy code The code is as follows:

Syntax:
$(function(){
var callback = function(r){ //r represents the data returned by the background data.
}
$.get("",callback); //Write the path of the method in quotation marks
}) ;

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


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

js:

Copy code The code is as follows:

$(function(){
var callback = function (r) {
var data = "
    "
            var data2 = "
    Discount list
    ";
    for (var i = 0; i < r.length; i ) {
         data = ("
  • " r[i]. CustomerName "Successfully applied for a trial and received a 10% discount on the annual service fee, congratulations!
  • ");

                                                                                                                                                                                                                                                                                     less for (var i = 0; i < r.length; i ) {
         if (i % 2 == 0) {
         t_data2 = "

                                                                                                                                                                                ; r.length - i - 1].CustomerName "
    ";
    }

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


    Backend code (mvc4):



    Copy code

    The code is as follows:public ActionResult TryCompany()                                              string CustomerType = Res.CustomerType.TestCustomer.ToString(); IList vlist = o_Res.GetHtmlCustomers(1, 100, out pCount, (a => a.CustomerType == CustomerType));
    var list = vlist.ToList().ConvertAll(s =>
    -dd HH:mm") : "" };
               });
                           return Json(list, JsonRequestBehavior.AllowGet); Call, traverse, and then display on the front end.
    A very simple example, there must be an easier way, I’ll dig into it later.

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn