茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    使用jQuery處理AJAX請求的基礎(chǔ)學習教程
    來源:易賢網(wǎng) 閱讀:1110 次 日期:2016-07-01 14:40:25
    溫馨提示:易賢網(wǎng)小編為您整理了“使用jQuery處理AJAX請求的基礎(chǔ)學習教程”,方便廣大網(wǎng)友查閱!

    這篇文章主要介紹了使用jQuery處理AJAX請求的基礎(chǔ)學習教程,除此之外還引申了鏈式處理事件回調(diào)的寫法,由淺入深非常值得借鑒,需要的朋友可以參考下

    $.ajax快捷方法

    $.get(url,[data],[callback],[type])

    $.post(url,[data],[callback],[type])

    兩種方法請求方式不同,其他方式相同.

    參數(shù):url[請求地址],data[請求的數(shù)據(jù)內(nèi)容(obj對象)],callback[回調(diào)函數(shù)(只能處理請求成功事件)],

    type[請求返回數(shù)據(jù)的編碼格式(默認ContentType指定格式)]

    $.get('/test?x=1');

    $.get('/test',{z:2});

    $.post('/test',{y:2});

    $.get('/user',function(data,callbacktype,jqXHR){

      data//返回數(shù)據(jù)

      callbacktype//返回數(shù)據(jù)的狀態(tài)信息(字符串)

      jqXHR//jQuery封裝的XHR對象

    });

    $(selector).load(url,[data],[callback])

    將頁面片段載入到selector的容器里面

    $("#content").load('/user');

    $.getJSON(url,[data],[callback])

    如果是JSON數(shù)據(jù)回調(diào)會成功,否則失敗

    $.getJSON('/test',{type:1},function(){

      console.log(argument)

    });

    $.getScript(url,[claaback])

    動態(tài)加載腳本文件

    $.gerScript('/js/test.js',function(){

      alert(test(1,2));

    });

    $.ajax詳細使用方法

    $.ajax(url,[settings]);

    $.ajax({

      url:'/test',

      success:function(){

        alert('ok');

      }

    });

    處理響應結(jié)果的回調(diào)函數(shù):

    success[成功],error[請求失敗],

    statusCode[指明返回的狀態(tài)碼的回調(diào)函數(shù)],

    complete[請求返回前的回調(diào)函數(shù)(處理返回不同狀態(tài)碼的請求)]

    $.ajax('/test',{

      success:function(data){

        console.log(arguments);

      },

      error:function(jqXHR,textStatus,err){

        //jqXHR:jQuery增強的XHR

        //textStatus:請求完成狀態(tài)

        //err:底層通過throw拋出的異常對象,類型與值與錯誤類型有關(guān)

        console.log(arguments);

      },

      complete:function(jqXHR,textStatus){

        //jqXHR:jQuery增強的XHR

        //textStatus:請求完成狀態(tài)success | error

        console.log(arguments);

      },

      statusCode:function(){

        '403':function(jqXHR,textStatus,err){

          //jqXHR:jQuery增強的XHR

          //textStatus:請求完成狀態(tài)

          //err:底層通過throw拋出的異常對象,類型與值與錯誤類型有關(guān)

          console.log(arguments);

          console.log(400);

        },

        '400':function(){

          console.log(400);

        }

      }   

    });

    請求的數(shù)據(jù):data,processData,contentType,traditional

    $.ajax('/test',{

      //請求的數(shù)據(jù)內(nèi)容

      data:{

        a:1,

        b:2

      },

      //請求的方式

      type:'POST',

      //是否對請求的數(shù)據(jù)進行轉(zhuǎn)碼(用于傳輸數(shù)據(jù)為html節(jié)點內(nèi)容)

      processData:true,

      //當前的數(shù)據(jù)是否使用傳統(tǒng)方式進行url編碼

      traditional:true,

      //請求數(shù)據(jù)編碼格式

      contentType:'application/json'

    });

    響應數(shù)據(jù):dataType,dataFilter

    $.ajax('/test',{

      success:function(data){

        console.log(typeof data)

      },

      //定義的返回數(shù)據(jù)的類型

      dataType:'json | html | text | jsonp | script',

      //返回底層的原始數(shù)據(jù)進行預處理

      dataFilter:function(data,type){

        //data:原始數(shù)據(jù)

        //type:指定的數(shù)據(jù)類型

      }  

    });

    前置處理:beforeSend

    $.ajax('/test',{

      beforeSend:function(jqXHR,settings){

        console.log(arguments);

        jqXHR.setRequestHeader('test','haha');

        jqXHR.testData = {a:1,b:2};

      },

      complete:function(jqXHR){

        console.log(jqXHR.testData)

      }

    });

    請求類型:GET(默認) | POST | PUT | DELETE

    同步異步:async(默認true)

    是否緩存:cache(默認true)

    其他參數(shù):

    1.global[是否觸發(fā)全局事件]

    2.ifModifed[僅在服務器數(shù)據(jù)改變時候加載數(shù)據(jù)]

    3.username,password[http需要驗證時候]

    4.timeout[設(shè)置請求超時時間,若請求超時觸發(fā)error]

    5.context[回調(diào)中this指向的對象]

    其他相關(guān)的API

    $.ajaxSetup(option)

    設(shè)置全局默認參數(shù)

    //默認為get請求

    $.ajax('/test');

    //修改全局請求方式為post

    $.ajaxSetup({

      type:'post',

      headers:{

        test:new Date().getTime

      },

      cache:false

    });

    //請求方式改變?yōu)閜ost

    $.ajax('/test');

    $.ajaxPrefilter([dataTypes],handler(option,originalOptions,jqXHR))

    請求發(fā)起前的預處理,提供了一種AOP(面向切面)編程模式,常見用途:

    1.根據(jù)option設(shè)定執(zhí)行特定處理邏輯

    2.修改option值改變請求默認行為

    3.通過return修改默認dataType

    例:通過return修改默認dataType

    $.ajaxPrefilter('text html json',function(options,originalOptions,jqXHR){

      //options請求參數(shù),含默認值

      //originalOptions請求時傳入的參數(shù),不含默認值

      //jqXHR:jQuery增強的XHR

      console.log(arguments);

      if(options.url == '/test'){

        return 'text';

      }

    });

    $.ajax('/test',{

      type:'post',

      dataType:'text',

      //自定義屬性

      test:'haha'

    });

    例:多次請求僅最后一次有效,避免多次請求導致的數(shù)據(jù)混亂

    var requests = {};

    $.ajaxPrefilter(function(options,originalOptions,jqXHR){

      if(requests[options.url]){

        requests[options.url].abort();

      }

      requests[options.url] = jqXHR;

    });

    $.ajax('/test/');

    $.ajax('/test/');

    例:統(tǒng)一修改請求路徑

    $.ajaxPrefilter(function(options){

      if(options.url.substr(0,5) == '/usr'){

        options.url = options.url.replace('/usr/','/user/');

        options.header = {

          a:1

        }

      }

    });

    $.ajax('/usr/');

    全局事件

    jQuery-1.9以后,全局事件必須綁定在document上

    $(document).ajaxSuccess(globalEventHander);

    $(document).ajaxError(globalEventHander);

    $(document).ajaxComplete(globalEventHander);

    $(document).ajaxStart(globalEventHander);

    $(document).ajaxStop(globalEventHander);

    $(document).ajaxSend(globalEventHander);

    function globalEventHander(event){

      console.log(arguments);

      console.log(event.type);

    }

    $.ajax('/test?err=y');//請求成功

    $.ajax('/test?err=n');//請求失敗

    //請求順序:

    //ajaxStart >> ajaxSend >> ajaxSend >> ajaxSuccess >> ajaxComplete >> ajaxError >> ajaxComplete >> ajaxStop

    序列化

    1.param[序列化一個 key/value 對象]

    2.serialize[通過序列化表單值,創(chuàng)建 URL 編碼文本字符串]

    3.serializeArray[通過序列化表單值來創(chuàng)建對象數(shù)組(名稱和值)]

    例:param()

    var params = { a:1, b:2 };

    var str = $.param(params);

    console.log(str);

    //a=1&b=2"

    例:serialize()

    <form>

      <div><input type="text" name="a" value="1"/></div>

      <div><input type="text" name="b" value="2"/></div>

      <div><input type="hidden" name="c" value="3"/></div>

    </form>

    <script type="text/javascript">

      $('form').submit(function() {

       console.log($(this).serialize());

       //a=1&b=2&c=3

       return false;

      });

    </script>

    例:serializeArray()

    <form>

      First:<input type="text" name="First" value="1" /><br />

      Last :<input type="text" name="Last" value="2" /><br />

    </form>

    <script type="text/javascript">

      $('form').click(function(){

        x=$("form").serializeArray();

        console.log(x);

        //{[name:First,value:1],[name:Last,value:2]}

      });

    </script>

    ajax鏈式編程方法

    在開發(fā)的過程,經(jīng)常會遇到一些耗時間的操作,比如ajax讀取服務器數(shù)據(jù)(異步操作),遍歷一個很大的數(shù)組(同步操作)。不管是異步操作,還是同步操作,總之就是不能立即得到結(jié)果,JS是單線程語音,不能立即得到結(jié)果,便會一直等待(阻塞)。

    一般的做法就是用回調(diào)函數(shù)(callback),即事先定義好一個函數(shù),JS引擎不等待這些耗時的操作,而是繼續(xù)執(zhí)行下面的代碼,等這些耗時操作結(jié)束后,回來執(zhí)行事先定義好的那個函數(shù)。如下面的ajax代碼:

    $.ajax({

      url: "test.html",

      success: function(){

        console.log("success");

      },

      error: function(){

        console.log("error");

      }

    });

    但這樣寫不夠強大靈活,也很啰嗦。為此,jQuery1.5版本引入Deferred功能,為處理事件回調(diào)提供了更加強大而靈活的編程模式。

    $.ajax("test.html")

    .done(

      function(){

        console.log("success");

      }

    )

    .fail(

      function(){

        console.log("error");

      }

    );

    不就是鏈式調(diào)用嘛,有何優(yōu)點?

    優(yōu)點一:可以清晰指定多個回調(diào)函數(shù)

    function fnA(){...}

    function fnB(){...}

    $.ajax("test.html").done(fnA).done(fnB);

    試想一下,如果用以前的編程模式,只能這么寫:

    function fnA(){...}

    function fnB(){...}

    $.ajax({

      url: "test.html",

      success: function(){

        fnA();

        fnB();

      }

    });

    優(yōu)點二:為多個操作指定回調(diào)函數(shù)

    $.when($.ajax("test1.html"), $.ajax("test2.html"))

    .done(function(){console.log("success");})

    .fail(function(){console.log("error");});

    用傳統(tǒng)的編程模式,只能重復寫success,error等回調(diào)了。

    更多信息請查看網(wǎng)絡編程
    易賢網(wǎng)手機網(wǎng)站地址:使用jQuery處理AJAX請求的基礎(chǔ)學習教程

    2026上岸·考公考編培訓報班

    • 報班類型
    • 姓名
    • 手機號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)