如何在c#中编写ajax函数

时间:2016-05-09 07:45:40

标签: c#

下面的函数如何在c#后端写入。 Post方法通过获取列表。

$(function () {
    $.ajax({
        type: "POST",
        url: "Default.aspx/GetCustomers",
        data: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function (response) {
            alert(response.d);
        },
        error: function (response) {
            alert(response.d);
        }
    });
});

1 个答案:

答案 0 :(得分:0)

您应该在后端拥有客户模型,然后您可以尝试:

    [HttpPost]
     public string GetCustomers()
     {
         List<Customers> listCustomers = new List<Customers>(); 
          /* the list of customers that is returned from the 
            database or from where you are picking the data*/

        CustomersViewModel model = new CustomersViewModel(){
                customers = listCustomers;
            }   

         return Json(model, JsonRequestBehavior.AllowGet);          
     }