Jquery .ajax方法分析(二)

来源:互联网 发布:淘宝客佣金代扣时间 编辑:程序博客网 时间:2024/06/03 03:51


http://www.cnblogs.com/jams742003/archive/2009/12/30/1636185.html


访问ws,而web方法再是字符串返回类型。这次通过response来响应请求。所处环境:.net3.5,而webservice 不再添加修饰标签:[System.Web.Script.Services.ScriptService]

(一)Hello

·ws

[WebMethod]

    public void HelloWorld()

    {

        HttpResponse Response =HttpContext.Current.Response;

        Response.ContentEncoding = System.Text.Encoding.Default;

        Response.ContentType = "application/json";

        Response.Write("Hello World!");

    }

 

·ajax post

function ajax_wsHell() {

    $.ajax({

        type: "post",

        contentType: "application/json",

        url: "ajax_2.asmx/HelloWorld",

        success: function(data) {

            alert(data);

        }

    });

}

 

Hello World!

 

·对于纯字串来说(不是json字串),响应的数据是真的字串。

 

(二)Customer

·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);

    }

·ajax post

function ajax_wsCustomer() {

    $.ajax({

        type: "post",

        contentType: "application/json",

        url: "ajax_2.asmx/GetCustomer",

        success: function(data) {

            var jsonObject = $.jsonToObject(data);

            var tt = '';

            $.each(jsonObject, function(k, v) {

                tt += k + "" + v +"<br/>";

            });

            $("#divmessage").html(tt);

        }

    });

}

 

{"Unid":1,"CustomerName":"宋江","Memo":"天魁星","Other":"黑三郎"}

 

对于json字串来说,它返回的是一个纯的json字串,不再是以d为key的k/v对。而对于json字串来说,转换到json对象很容易(. jsonToObject()方法,我以前的随笔中有介绍)。

(三)customer list

·ws

[WebMethod]

    public void GetCustomerList()

    {

        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 ajax_wsCustomerList() {

    $.ajax({

        type: "post",

        contentType: "application/json",

        url: "ajax_2.asmx/GetCustomerList",

        success: function(data) {

            var jsonObject = $.jsonToObject(data);

            var tt = '';

            $.each(jsonObject, function(k, v) {

            $.each(v, function(kk, vv) {

            tt += kk + "" + vv +"<br/>";

                });

            });

            $("#divmessage").html(tt);

        }

    });

}

 

[

{"Unid":1,"CustomerName":"宋江","Memo":"天魁星","Other":"黑三郎"},

{"Unid":2,"CustomerName":"吴用","Memo":"天机星","Other":"智多星"}

]

 

可以看出,得到的也是纯json字串。

 

(四)带参数

·ws

[WebMethod]

    public void GetCustomerListWithPara(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 cus = from q in _list

                  where q.Unid == iUnid

                  select q;

 

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

 

        HttpResponse Response =HttpContext.Current.Response;

        Response.ContentEncoding = System.Text.Encoding.Default;

        Response.ContentType = "application/json";

        Response.Write(strJson);

    }

 

·ajax post

 

function ajax_wsCustomerListWithPara() {

    $.ajax({

        type: "post",

        url: "ajax_2.asmx/GetCustomerListWithPara",

        data: ({ iUnid: 1 }),

        dataType: "json",

        success: function(data) {

            var tt = '';

            $.each(data, function(k, v) {

                $.each(v, function(kk, vv) {

                    tt += kk + "" + vv +"<br/>";

                });

            });

            $("#divmessage").html(tt);

        }

    });

}

 

[{"Unid":1,"CustomerName":"宋江","Memo":"天魁星","Other":"黑三郎"}]

 

这里又返回了一个json对象。而不是一个json串。而且,没有给客户端ajax请求添加 contentType参数。

 

综述:

·对于请求由Response响应的json字串值,有很大的便利性,但对于带参数的请求,返回的是一个json对象,这可以直接处理。

·而web服务类不必添加修饰标签,这在.net2.0中应该也是可以的,是普遍的。所以,在通过web服务实现ajax时,可以采用这一通用方法。

·.ajax()方法的参数要合适,否则会不出结果,其根源在于jquery类库中对function :ajax的定义

·ajax方法是.getJSON(),.get(),.post()的根。而这三种方法是做为.ajax方法的一种特殊情况集来表达的。



0 0