1. 程式人生 > >原生Ajax XMLHttpRequest物件

原生Ajax XMLHttpRequest物件

一、Ajax請求


- 現在常見的前後端分離專案中,一般都是伺服器返回靜態頁面後
瀏覽器載入完頁面,執行script中的js程式碼,通過ajax向後端api傳送非同步請求
獲取資料,然後呼叫回撥函式,將資料新增到頁面上  

 

1.1 JQuery封裝後的ajax請求


 

- 注意:jQuery Ajax本質 XMLHttpRequest 或 ActiveXObject 

 1 jQuery.get(...)
 2     所有引數:
 3          url: 待載入頁面的URL地址
 4         data: 待發送 Key/value 引數。
 5
success: 載入成功時回撥函式。 6 dataType: 返回內容格式,xml, json, script, text, html 7 8 9 jQuery.post(...) 10 所有引數: 11 url: 待載入頁面的URL地址 12 data: 待發送 Key/value 引數 13 success: 載入成功時回撥函式 14 dataType: 返回內容格式,xml, json, script, text, html 15 16 17 jQuery.getJSON(...)
18 所有引數: 19 url: 待載入頁面的URL地址 20 data: 待發送 Key/value 引數。 21 success: 載入成功時回撥函式。 22 23 24 jQuery.getScript(...) 25 所有引數: 26 url: 待載入頁面的URL地址 27 data: 待發送 Key/value 引數。 28 success: 載入成功時回撥函式。 29 30 31 jQuery.ajax(...) 32 33 部分引數:
34 35 url:請求地址 36 type:請求方式,GET、POST(1.9.0之後用method) 37 headers:請求頭 38 data:要傳送的資料 39 contentType:即將傳送資訊至伺服器的內容編碼型別(預設: "application/x-www-form-urlencoded; charset=UTF-8") 40 async:是否非同步 41 timeout:設定請求超時時間(毫秒) 42 43 beforeSend:傳送請求前執行的函式(全域性) 44 complete:完成之後執行的回撥函式(全域性) 45 success:成功之後執行的回撥函式(全域性) 46 error:失敗之後執行的回撥函式(全域性) 47 48 49 accepts:通過請求頭髮送給伺服器,告訴伺服器當前客戶端課接受的資料型別 50 dataType:將伺服器端返回的資料轉換成指定型別 51 "xml": 將伺服器端返回的內容轉換成xml格式 52 "text": 將伺服器端返回的內容轉換成普通文字格式 53 "html": 將伺服器端返回的內容轉換成普通文字格式,在插入DOM中時,如果包含JavaScript標籤,則會嘗試去執行。 54 "script": 嘗試將返回值當作JavaScript去執行,然後再將伺服器端返回的內容轉換成普通文字格式 55 "json": 將伺服器端返回的內容轉換成相應的JavaScript物件 56 "jsonp": JSONP 格式 57 使用 JSONP 形式呼叫函式時,如 "myurl?callback=?" jQuery 將自動替換 ? 為正確的函式名,以執行回撥函式 58 59 如果不指定,jQuery 將自動根據HTTP包MIME資訊返回相應型別(an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string 60 61 converters: 轉換器,將伺服器端的內容根據指定的dataType轉換型別,並傳值給success回撥函式 62 $.ajax({ 63 accepts: { 64 mycustomtype: 'application/x-some-custom-type' 65 }, 66 67 // Expect a `mycustomtype` back from server 68 dataType: 'mycustomtype' 69 70 // Instructions for how to deserialize a `mycustomtype` 71 converters: { 72 'text mycustomtype': function(result) { 73 // Do Stuff 74 return newresult; 75 } 76 }, 77 });
View Code
jQuery.get(...)
    所有引數:
         url: 待載入頁面的URL地址
        data: 待發送 Key/value 引數。
     success: 載入成功時回撥函式。
    dataType: 返回內容格式,xml, json,  script, text, html


jQuery.post(...)
    所有引數:
         url: 待載入頁面的URL地址
        data: 待發送 Key/value 引數
     success: 載入成功時回撥函式
    dataType: 返回內容格式,xml, json,  script, text, html


jQuery.getJSON(...)
    所有引數:
         url: 待載入頁面的URL地址
        data: 待發送 Key/value 引數。
     success: 載入成功時回撥函式。


jQuery.getScript(...)
    所有引數:
         url: 待載入頁面的URL地址
        data: 待發送 Key/value 引數。
     success: 載入成功時回撥函式。


jQuery.ajax(...)

    部分引數:

            url:請求地址
           type:請求方式,GET、POST(1.9.0之後用method)
        headers:請求頭
           data:要傳送的資料
    contentType:即將傳送資訊至伺服器的內容編碼型別(預設: "application/x-www-form-urlencoded; charset=UTF-8")
          async:是否非同步
        timeout:設定請求超時時間(毫秒)

     beforeSend:傳送請求前執行的函式(全域性)
       complete:完成之後執行的回撥函式(全域性)
        success:成功之後執行的回撥函式(全域性)
          error:失敗之後執行的回撥函式(全域性)
    

        accepts:通過請求頭髮送給伺服器,告訴伺服器當前客戶端課接受的資料型別
       dataType:將伺服器端返回的資料轉換成指定型別
                       "xml": 將伺服器端返回的內容轉換成xml格式
                      "text": 將伺服器端返回的內容轉換成普通文字格式
                      "html": 將伺服器端返回的內容轉換成普通文字格式,在插入DOM中時,如果包含JavaScript標籤,則會嘗試去執行。
                    "script": 嘗試將返回值當作JavaScript去執行,然後再將伺服器端返回的內容轉換成普通文字格式
                      "json": 將伺服器端返回的內容轉換成相應的JavaScript物件
                     "jsonp": JSONP 格式
                              使用 JSONP 形式呼叫函式時,如 "myurl?callback=?" jQuery 將自動替換 ? 為正確的函式名,以執行回撥函式

                      如果不指定,jQuery 將自動根據HTTP包MIME資訊返回相應型別(an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string

     converters: 轉換器,將伺服器端的內容根據指定的dataType轉換型別,並傳值給success回撥函式
             $.ajax({
                  accepts: {
                    mycustomtype: 'application/x-some-custom-type'
                  },
                  
                  // Expect a `mycustomtype` back from server
                  dataType: 'mycustomtype'

                  // Instructions for how to deserialize a `mycustomtype`
                  converters: {
                    'text mycustomtype': function(result) {
                      // Do Stuff
                      return newresult;
                    }
                  },
                });

  

 

1.2.2 一個簡單的Ajax請求