Keyboard shortcuts

Press ← or → to navigate between chapters

Press ? to show this help

Press Esc to hide this help

RequestTask mc.request(Object object)

发起 HTTPS 网络请求。

Object object 参数

属性类型默认值必填说明最低版本
urlString是开发者服务器接口地址
dataString/Object/ArrayBuffer否请求的参数
headerObject否设置请求的 header。 content-type 默认为 application/json
timeoutNumber否超时时间,单位为毫秒
methodStringGET否HTTP 请求方法
dataTypeStringjson否返回的数据格式 json 会对返回的数据进行一次 JSON.parse
responseTypeStringtext否响应的数据类型
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行) 

注意:最终发送给服务器的数据是 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)...)

Object.method 的合法值

值说明最低版本
OPTIONSHTTP 请求 OPTIONS
GETHTTP 请求 GET
HEADHTTP 请求 HEAD
POSTHTTP 请求 POST
PUTHTTP 请求 PUT
DELETEHTTP 请求 DELETE
TRACEHTTP 请求 TRACE
CONNECTHTTP 请求 CONNECT 

Object.responseType 的合法值

值说明最低版本
text响应的数据为文本
Arraybuffer响应的数据为 ArrayBuffer 

Object.success(Object res) 回调函数

属性类型说明最低版本
dataString/Object/Arraybuffer开发者服务器返回的数据
statusCodeNumber开发者服务器返回的 HTTP 状态码
headerObject开发者服务器返回的 HTTP Response Header
cookies[String]开发者服务器返回的 cookies,格式为字符串数组
profileObject网络请求过程中一些调试信息 

返回值

类型说明
RequestTask请求任务对象

请求任务对象

示例代码:

mc.request({
  url: 'https://test.com',
  data: {
    user: 'test',
    pwd: 'pwd'
  },
  method: 'POST',
  success: function (e) {
    console.log(e.data);
  }
})