Jquery .ajax方法分析(一)

简介: jQuery.ajax( options )   有很多选项,介绍其中的几个: ·dataType:想从服务器得到哪种类型的数据。xml,html,script,json,jsonp,text ·success:请求成功后的处理函数 ·type:以POST或GET的方式请求。

jQuery.ajax( options )

 

有很多选项,介绍其中的几个:

·dataType:想从服务器得到哪种类型的数据。xml,html,script,json,jsonp,text

·success:请求成功后的处理函数

·type:以POSTGET的方式请求。默认GETPUTDELETE也可以用,但并不是所有的浏览器都支持

·url:请求的目的地址,须是一个字符串。

·complete:不管请求成功还是错误,只要请求完成,可以执行的事件。

·beforeSend:传递异步请求之前的事件。

 

这次说解,使用firedebug来配合说解。

(一)请求ashx文件,并添加ajax事件,添加缓冲提示

描述:请求数据,请求超时时间设置为5秒,如果超时,那么输出超时提示,且在这5秒中的等待过程中,提供等图标,5秒之后,提示请求超时。

1ashx文件

Customer customer = new Customer

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

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

        System.Threading.Thread.Sleep(1000*7);

        context.Response.Write(strJson);

为了让客户端请求超时,服务设置延时7秒。

2ajax post请求

function ajax_ashx() {

    $.ajax({

        url: "webdata/ajax_1.ashx",

        type: "post",

        success: function(data) {

            var jsonObject = $.jsonToObject(data);

            var tt = '';

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

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

            });

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

        },

        cache: false,

        timeout: 5000,

        error: function() {

            alert("超时");

        }

    });}

设置超时时间5000ms,超时错误出示超时错误。

3)设置客户端请求等待图标

·<img src="images/loader.gif" id="ajaximg" /> 找个小图标

·为这个图标设置ajax事件

$("#ajaximg").bind("ajaxSend", function() { Show(); });

    $("#ajaximg").bind("ajaxComplete", function() { Hide(); });

 

function Hide() {

    $("#ajaximg").hide();

}

function Show() {

    $("#ajaximg").show();

}

在客户端5秒请求的时间限制下,请求超时,提示超时错误。

·ashxcontentType的设置对返回的数据没有影响

·客户端dataType也没有影响,可以省略。

·firebug里可以看到返回的数据为:

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

所以可以按以前我说过的方法进行解析。

(二)正常请求,并解析

·ashx

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(customer);

        System.Threading.Thread.Sleep(1000 * 3);

        context.Response.Write(strJson);

·ajax post

function ajax_ashxList() {

    $.ajax({

        url: "webdata/ajax_1.ashx",

        type: "post",

        dataType: "json",

        success: function(data) {

            var tt = '';

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

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

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

                });

            });

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

        },

        cache: false,

        timeout: 5000,

        error: function() {

            alert("超时");

        }

    });

}

·dataType要是json

·firebug

[

{"Unid":1,"CustomerName":"","Memo":"","Other":""},

{"Unid":2,"CustomerName":"","Memo":"","Other":""}

]

虽然是字串,但这里直接用就行,不用转换为json对象。这一点我现在还不明白怎么回事。

(三)请求ws

这次请求返回字串类型的web方法。

1Hello

[WebMethod]

    public string HelloWorld()

    {

       return "Hello World";

    }

 

function ajax_webserviceHello() {

    $.ajax({

        type: "post",

        contentType: "application/json",

        url: "ajax_1.asmx/HelloWorld",

        data: "{}",

        dataType: 'json',

        success: function(data) {

            alert(data.d);

        }

    });

}

·contentTypedata都不能为空,即使data为空,也要带空参数

 

{"d":"Hello World"}

发现服务端请求到的数据是这样的。所以,访问时,要以data.d来访问。(在.net3.5中)。但也可以如下访问:

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

                alert(v);

            });

 

2Customer

这次得到一个客户实体

[WebMethod]

    public string GetCustomer()

    {

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

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

        return strJson;

}

 

function ajax_webserviceCustomer() {

    $.ajax({

        type: "post",

        contentType: "application/json",

        url: "ajax_1.asmx/GetCustomer",

        data: "{}",

        dataType: 'json',

        success: function(data) {

            var tt = '';

            var jsonObject = $.jsonToObject(data.d);

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

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

            });

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

        }

    });

}

发现返回的也是以dkey的一个object

 

{"d":"{\"Unid\":1,\"CustomerName\":\"\",\"Memo\":\"\",\"Other\":\"\"}"}

这点应该注意。

3customer list

[WebMethod]

    public string 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);

        return strJson;

}

 

function ajax_webserviceCustomerList() {

    $.ajax({

        type: "post",

        contentType: "application/json",

        url: "ajax_1.asmx/GetCustomerList",

        data: "{}",

        dataType: 'json',

        success: function(data) {

            var tt = '';

            var jsonObject = $.jsonToObject(data.d);

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

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

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

                });

            });

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

        }

    });

}

 

 

{

"d":

"[{\"Unid\":1,\"CustomerName\":\"\",\"Memo\":\"\",\"Other\":\"\"},

{\"Unid\":2,\"CustomerName\":\"\",\"Memo\":\"\",\"Other\":\"\"}]"

}

这也是一个以dkey的对象。

4with para

[WebMethod]

    public string 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);

        return strJson;

    }

 

function ajax_webserviceCustomerListWithPara() {

    $.ajax({

        type: "post",

        contentType: "application/json",

        url: "ajax_1.asmx/GetCustomerListWithPara",

        data: "{iUnid:"+1+"}",

        dataType: 'json',

        success: function(data) {

            var tt = '';

            var jsonObject = $.jsonToObject(data.d);

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

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

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

                });

            });

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

        }

    });

}

 

{

"d":

"[{\"Unid\":1,\"CustomerName\":\"\",\"Memo\":\"\",\"Other\":\"\"}]"

}

这也是一个以dkey的对象。

综上所述,在对web服务进行请求时:

·.net3.5中,访问web服务时,返回的元素是一个以dkeyk/v对。如果要进行下一步解析,要认识d属性。(这是在当web方法返回json字串时成立)

·.net3.5中,访问web服务,要对web服务添加修饰:[System.Web.Script.Services.ScriptService] 否则,当.ajax()请求服务时,会有异常:

只能从脚本中调用在类定义上有[ScriptService]属性的 Web 服务

 

 

博客园大道至简

http://www.cnblogs.com/jams742003/

转载请注明:博客园

目录
相关文章
|
10天前
|
JavaScript 前端开发 容器
AJAX载入外部JS文件到页面并让其执行的方法(附源码)
AJAX载入外部JS文件到页面并让其执行的方法(附源码)
14 0
N..
|
25天前
|
XML JSON 前端开发
jQuery实现Ajax
jQuery实现Ajax
N..
16 1
|
3月前
|
JavaScript
jQuery追加节点方法 和height方法与width方法
jQuery追加节点方法 和height方法与width方法
|
3月前
|
JavaScript 前端开发
调用jQuery的animate()方法无法移动的问题
调用jQuery的animate()方法无法移动的问题
|
3月前
|
JavaScript 前端开发 UED
jQuery 自动刷新页面但不闪烁的实现方法
jQuery 自动刷新页面但不闪烁的实现方法
|
1月前
|
前端开发
AJAX发送请求方法封装和请求函数底层刨析以及axios二次封装
AJAX发送请求方法封装和请求函数底层刨析以及axios二次封装
|
1月前
|
移动开发 前端开发 安全
Ajax跨域的所有方法(最详细带使用教程!!!)
Ajax跨域的所有方法(最详细带使用教程!!!)
|
2月前
|
JavaScript 前端开发 Java
jquery ajax+spring mvc上传文件
jquery ajax+spring mvc上传文件
|
2月前
|
数据采集 Web App开发 前端开发
Python爬虫之Ajax分析方法与结果提取#6
Ajax分析方法、Ajax结果提取【2月更文挑战第20天】
36 0
Python爬虫之Ajax分析方法与结果提取#6
|
2月前
|
XML 前端开发 JavaScript
AJAX get() 和 post() 方法
AJAX(Asynchronous JavaScript and XML)是一种用于创建快速和动态网页的技术,它允许使用 JavaScript 和 XMLHttpRequest 对象在不重新加载整个页面的情况下向服务器发送请求和接收响应。jQuery 提供了几个用于 AJAX 操作的方法,包括 .ajax()、.get() 和 .post()
14 1