Skip to content

發起請求

request

TIP

該API使用方法為: RequestTask wx.request(Object object)

  • 功能說明: 發起HTTPS網絡請求。 使用前請注意閱讀相關說明。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    urlstring-開發者服務器接口地址
    datastring/object/ArrayBuffer-請求的參數
    headerObject-設定請求的header,header中不能設定Referer。 content-type默認為application/json
    timeoutnumber-超時時間,單位為毫秒
    methodstringGETHTTP請求方法
    dataTypestringjson返回的數據格式
    responseTypestringtext響應的數據類型
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
    • object.method的合法值:
    說明
    OPTIONSHTTP請求OPTIONS
    GETHTTP請求GET
    HEADHTTP請求HEAD
    POSTHTTP請求POST
    PUTHTTP請求PUT
    DELETEHTTP請求DELETE
    TRACEHTTP請求TRACE
    CONNECTHTTP請求CONNECT
    • object.dataType的合法值:
    說明
    json返回的數據為JSON,返回後會對返回的數據進行一次JSON.parse
    其他不對返回的內容進行JSON.parse
    • object.responseType的合法值:
    說明
    text響應的數據為文字
    arraybuffer響應的數據為ArrayBuffer
    • object.responseType的合法值:
    說明
    text響應的數據為文字
    arraybuffer響應的數據為ArrayBuffer
  • object.success回呼函數參數:

    參數: Object res

    内容類型說明
    datastring/Object/Arraybuffer開發者服務器返回的數據
    statusCodenumber開發者服務器返回的HTTP狀態碼
    headerObject開發者服務器返回的HTTP Response Header
    cookiesArray.<string>開發者服務器返回的cookies,格式為字串數組
  • object.fail回呼函數參數:

    參數: Object err

    内容類型說明
    errMsgString錯誤資訊
  • 返回值: RequestTask ,請求任務對象。

  • data參數說明:

    最終發送給服務器的數據是String類型,如果傳入的data不是String類型,會被轉換成String,轉換規則如下:

    • 對於GET方法的數據,會將資料轉換成query string(encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)…)
    • 對於POST方法且header['content-type']為application/json的數據,會對數據進行JSON序列化。
    • 對於POST方法且header['content-type']為application/x-www-form-urlencoded的數據,會將資料轉換成query string(encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)…)
  • 示例代碼:

js
wx.request({
  url: 'test.php', // This is merely an example and not an actual interface address.
  data: {
    x: '',
    y: ''
  },
  header: {
    'content-type': 'application/json' // Default value
  },
  success(res) {
    console.log(res.data)
  }
})

RequestTask

.abort

TIP

該API使用方法為: RequestTask.abort()

  • 功能說明: 插斷要求任務

.onChunkReceived

TIP

該API使用方法為: RequestTask.onChunkReceived(function listener)

  • 功能說明: 監聽Transfer-Encoding Chunk Received事件。 當接收到新的chunk時觸發。

  • 參數及說明: function listener,Transfer-Encoding Chunk Received事件的監聽函數,參數Object res如下:

    内容類型說明
    resObject開發者服務器每次返回新chunk時的Response
  • res結構内容

    内容類型說明
    dataArrayBuffer返回的chunk buffer

.offChunkReceived

TIP

該API使用方法為: RequestTask.offChunkReceived(function listener)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 移除Transfer-Encoding Chunk Received事件的監聽函數。

  • 參數及說明: function listener, onChunkReceived傳入的監聽函數,不傳此參數則移除所有監聽函數。

  • 示例代碼:

js
const listener = function (res) { console.log(res) }

RequestTask.onChunkReceived(listener)
RequestTask.offChunkReceived(listener)  // Pass the same function object used for listening.

.onHeadersReceived

TIP

該API使用方法為: RequestTask.onHeadersReceived(function listener)

  • 功能說明: 監聽HTTP Response Header事件。 會比請求完成事件更早。

  • 參數及說明: function listener,HTTP Response Header事件的監聽函數。

    内容類型說明
    headerObject開發者服務器返回的HTTP Response Header
    statusCodeNumber開發者服務器返回的HTTP狀態碼(現時開發者工具上不會返回statusCode欄位,可用真機查看該欄位,後續將會支持)
    cookiesArray.<string>開發者服務器返回的cookies,格式為字串數組

.offHeadersReceived

TIP

該API使用方法為: RequestTask.offHeadersReceived(function listener)

  • 功能說明: 移除HTTP Response Header事件的監聽函數。

  • 參數及說明: function listener, onHeadersReceived傳入的監聽函數,不傳此參數則移除所有監聽函數。

  • 示例代碼:

js
const listener = function (res) { console.log(res) }

RequestTask.onHeadersReceived(listener)
RequestTask.offHeadersReceived(listener) // Pass the same function object used for listening.