jQ-ajax

  • ajaxStart (Global Event)//

    无论在何时发送 Ajax 请求,jQuery 都会检查是否存在其他 Ajax 请求。如果不存在,则 jQuery 会触发该 ajaxStart 事件。在此时,由 .ajaxStart() 方法注册的任何函数都会被执行。

    当 AJAX 请求开始时,显示“加载中”的指示:

    $("div").ajaxStart(function(){
      $(this).html("<img src='demo_wait.gif' />");
    });
    This event is triggered if an Ajax request is started and no other Ajax requests are currently running.
    • beforeSend (Local Event)
    • 类型: Function( jqXHR jqXHR, PlainObject settings )
      请求发送前的回调函数,用来修改请求发送前jqXHR(在jQuery 1.4.x的中,XMLHttpRequest)对象,此功能用来设置自定义 HTTP 头信息,等等。该jqXHR和设置对象作为参数传递。这是一个 Ajax事件 。在 beforeSend函数中返回 false将取消这个请求。 从jQuery 1.5开始, beforeSend选项将被访问,不管请求的类型。
      This event, which is triggered before an Ajax request is started, allows you to modify the XMLHttpRequest object (setting additional headers, if need be.)
  • ajaxSend (Global Event)
  • 当 AJAX 请求即将发送时,改变 div 元素的内容:

    $("div").ajaxSend(function(e,xhr,opt){
      $(this).html("Requesting " + opt.url);
    });
    

    This global event is also triggered before the request is run.
  • success (Local Event)
  • 当请求成功并接收到返回数据之后调用
    This event is only called if the request was successful (no errors from the server, no errors with the data).
  • ajaxSuccess (Global Event)
  • 当 AJAX 请求成功完成时,触发提示框:

    $("div").ajaxSuccess(function(){
      alert("AJAX 请求已成功完成");
    });
    This event is also only called if the request was successful.
  • error (Local Event)
  • 类型: Function( jqXHR jqXHR, String textStatus, String errorThrown )
    请求失败时调用此函数。有以下三个参数:jqXHR (在 jQuery 1.4.x前为XMLHttpRequest) 对象、描述发生错误类型的一个字符串 和 捕获的异常对象。如果发生了错误,错误信息(第二个参数)除了得到 null之外,还可能是 "timeout", "error", "abort" ,和 "parsererror"。 当一个HTTP错误发生时, errorThrown 接收HTTP状态的文本部分,比如: "Not Found"(没有找到) 或者 "Internal Server Error."(服务器内部错误)。 从jQuery 1.5开始, 在 error设置可以接受函数组成的数组。每个函数将被依次调用。 注意: 此处理程序在跨域脚本和JSONP形式的请求时不被调用。这是一个 Ajax Event。                        
    This event is only called if an error occurred with the request (you can never have both an error and a success callback with a request).
  • ajaxError (Global Event)
  • 当 AJAX 请求失败时,触发提示框:

    $("div").ajaxError(function(){
      alert("An error occurred!");
    });
    This global event behaves the same as the local error event.
  • complete (Local Event)
  • 类型: Function( jqXHR jqXHR, String textStatus )
    请求完成后回调函数 (请求 successerror之后均调用)。这个回调函数得到2个参数: jqXHR (在 jQuery 1.4.x中是 XMLHTTPRequest) 对象和一个描述请求状态的字符串( "success", "notmodified", "error", "timeout", "abort", 或者 "parsererror") 。 从jQuery 1.5开始, complete设置可以接受一个函数的数组。每个函数将被依次调用。这是一个 Ajax事件 。 
    This event is called regardless of if the request was successful, or not. You will always receive a complete callback, even for synchronous requests.
  • ajaxComplete (Global Event)
  • 当 AJAX 请求正在进行时显示“正在加载”的指示:

    $("#txt").ajaxStart(function(){
      $("#wait").css("display","block");
    });
    $("#txt").ajaxComplete(function(){
      $("#wait").css("display","none");
    });
    This event behaves the same as the complete event and will be triggered every time an Ajax request finishes.
  • ajaxStop (Global Event)
  • 当所有 AJAX 请求完成时,触发一个提示框:

    $("div").ajaxStop(function(){
      alert("所有 AJAX 请求已完成");
    });
    This global event is triggered if there are no more Ajax requests being processed.


  • 通过 AJAX 加载一段文本:

    jQuery 代码:

    $(document).ready(function(){
      $("#b01").click(function(){
      htmlobj=$.ajax({url:"/jquery/test1.txt",async:false});
      $("#myDiv").html(htmlobj.responseText);//向ajax后台的程序发送xmlhttp请求的时候, 后台程序接到请求会进行处理,处理结束后,可以返回一串数据给前台,这个就是responseText.
      });
    });
    

    jquery中各个事件执行顺序如下:

    1.ajaxStart(全局事件)

    2.beforeSend

    3.ajaxSend(全局事件)

    4.success

    5.ajaxSuccess(全局事件)

    6.error

    7.ajaxError (全局事件)

    8.complete

    9.ajaxComplete(全局事件)

    10.ajaxStop(全局事件)


    从jQuery 1.5开始, faildone ,和从jQuery 1.6开始的always回调钩子(hooks)采用先入先出队列管理。这意味着你可以为每个挂钩分配多个回调。见Deferred object methods ,这是实现内部的$.ajax()回调钩子(hooks)。

    这里有$.ajax()提供的回调钩子 (hooks),如下:

    1. beforeSend 在发送请求之前调用,它接收jqXHR对象和settings作为参数对象。
    2. error 在请求出错时调用。如果请求失败,在它们的顺序依次登记。他们接受jqXHR ,字符串表示的错误类型,以及异常对象(如果有的话)。一些内置的错误会将 "abort", "timeout", "No Transport" 等字符串作为异常对象。
    3. dataFilter 在请求成功之后调用。传入返回的数据以及dataType参数的值。并且必须返回新的数据(可能是处理过的)传递给success回调函数。
    4. success 当请求成功并接收到返回数据之后调用。传入返回后的数据,以及包含成功代码的字符串和jqXHR对象。
    5. complete 请求完成时,无论是在失败或成功,它们按顺序依次执行回调。他们收到jqXHR对象,以及一个包含成功或错误代码。
    $(function(){
        $(document).ajaxStart(function(){
            console.log('yes');
        });
        $('body').load('./divStyle.html',{'name':0},function(){
            //console.log('yes');
        });
        $(document).ajaxStop(function(){//1.8版本以上的jquery中ajaxStart只支持绑定到document。
            console.log('No');
        });
    
    })
    .serializeArray()这个方法不接受任何参数。

  • 描述: 将用作提交的表单元素的值编译成拥有name和value对象组成的数组。例如[ { name: a value: 1 }, { name: b value: 2 },...]
  • jQuery.parseJSON( json )参数是要解析的 JSON 字符串。

  • 描述: 接受一个标准格式的 JSON 字符串,并返回解析后的 JavaScript 对象。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值