• Jquery的.post说解(二)


    $.post

    调用webservice,通过Response来返回数据。

    (一)Hello

    ·ws

    [WebMethod]
    public void HelloWorld()
    {
        HttpResponse Response 
    = HttpContext.Current.Response;
        Response.ContentEncoding 
    = System.Text.Encoding.Default;
        Response.Write(
    "Hello world!");
    }

    ·ajax post

    function ajaxVoidHello() {
        $.post(
        
    "post_2.asmx/HelloWorld",
        
    function(data) {
            
    var jsonString = data.text;
            $(
    "#divmessage").html(data);
        },
        
    "json"
        );
    }

    客户端得到的数据类型为string类型。

    (二)得到客户实体

    ·ws

    [WebMethod]
    public void GetCustomer()
    {
        Customer customer 
    = new Customer 
            { Unid 
    = 1, CustomerName = "宋江", Memo = "天魁星", Other = "黑三郎" };

        
    string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(customer); 

        HttpResponse Response 
    = HttpContext.Current.Response;
        Response.ContentEncoding 
    = System.Text.Encoding.Default;
        Response.ContentType 
    = "application/json";
        Response.Write(strJson);
    }

    这里ContentType很重要,这里要保留,即使空值也可以。

    ·ajax post

    function ajaxGetCustomer() {
        $.post(
        
    "post_2.asmx/GetCustomer",
        
    function(data) {
            
    var jsonString = data;
            
    var jsonObject = $.jsonToObject(jsonString); 

            
    var tt = '';
            $.each(jsonObject, 
    function(k, v) {
                tt 
    += k + ":" + v + "<br/>";
            });

            $(
    "#divmessage").html(tt);
        },
       
    "json"
    );}

    请求json数据,返回的是一个字符串,就是json字串,然后处理。

    (三)得到客户集

    ·ws

    [WebMethod]
    public void GetCustomersList()
    {
        Customer customer 
    = new Customer 
           { Unid 
    = 1, CustomerName = "宋江", Memo = "天魁星", Other = "黑三郎" };

        Customer customer2 
    = new Customer 
           { Unid 
    = 2, CustomerName = "吴用", Memo = "天机星", Other = "智多星" }; 

        List
    <Customer> _list = new List<Customer>();
        _list.Add(customer);
        _list.Add(customer2);
        
    string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(_list); 

        HttpResponse Response 
    = HttpContext.Current.Response;
        Response.ContentEncoding 
    = System.Text.Encoding.Default;
        Response.ContentType 
    = "application/json";
        Response.Write(strJson);
    }

    ·ajax post

    function ajaxGetCustomerList() {
        $.post(
        
    "post_2.asmx/GetCustomersList",
        
    function(data) {
            alert(data);
            
    var jsonString = data;
            
    var jsonObject = $.jsonToObject(jsonString); 

            
    var tt = '';
            $.each(jsonObject, 
    function(k, v) {
                $.each(v, 
    function(kk, vv) {
                    tt 
    += kk + ":" + vv + "<br/>";
                });
            });
            $(
    "#divmessage").html(tt);
        },
       
    "json"
    );}

    这些很容易理解了,返回的是json字串。处理字串就可以了。

    (四)带参数的

    ·ws

    [WebMethod]
    public void GetCustomersListWithPara(int iUnid)
    {

        Customer customer 
    = new Customer 
            { Unid 
    = 1, CustomerName = "宋江", Memo = "天魁星", Other = "黑三郎" };

        Customer customer2 
    = new Customer 
            { Unid 
    = 2, CustomerName = "吴用", Memo = "天机星", Other = "智多星" }; 

        List
    <Customer> _list = new List<Customer>();
        _list.Add(customer);
        _list.Add(customer2); 

        var q 
    = _list.Where(p => p.Unid == iUnid); 

        
    string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(q); 

        HttpResponse Response 
    = HttpContext.Current.Response;
        Response.ContentEncoding 
    = System.Text.Encoding.Default;
        Response.ContentType 
    = "";
        Response.Write(strJson);
    }

    ·ajax post

    function ajaxGetCustomerListWithPara() {
        $.post(
        
    "post_2.asmx/GetCustomersListWithPara",
        { iUnid: 
    1 },
        
    function(data) {
            
    var tt = '';
            $.each(data, 
    function(k, v) {
                $.each(v, 
    function(kk, vv) {
                    tt 
    += kk + ":" + vv + "<br/>";
                })
            });
            $(
    "#divmessage").html(tt);
        },
       
    "json"
    );}

    这里返回的是[object,object]类型。

    服务端的ContentType可以设置为空。 

     

  • 相关阅读:
    《互联网时代的软件革命-saas架构设计》读书笔记
    iphone11白苹果 解决办法最简单
    idea配置(重新配置)
    力扣刷题03--无重复字符的最长字串
    力扣刷题02--两数相加
    《设计原本》读书笔记(三)
    《设计原本》读书笔记(二)
    《设计原本》读书笔记(一)
    MVC架构科技小论文
    面向服务架构及其应用
  • 原文地址:https://www.cnblogs.com/jams742003/p/1635248.html
Copyright © 2020-2023  润新知