Jquery .ajax方法分析(二)‏

访问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字串,不再是以dkeyk/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方法的一种特殊情况集来表达的。

转载于:https://www.cnblogs.com/Love-lisa/archive/2012/05/16/2503613.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值