跳转到内容

数据请求

{ "name": "blueos.network.fetch" }
import fetch from '@blueos.network.fetch' const fetch = require('@blueos.network.fetch')

获取网络数据

参数名类型必填说明
urlString资源 url
dataString/Object/ArrayBuffer请求的参数,可以是字符串,或者是 js 对象、arraybuffer 对象。参考 data与Content-Type关系 部分
headerObject请求的 header,会将其所有属性设置到请求的 header 部分。User-Agent 设置在版本开始支持。示例:{“Accept-Encoding”: “gzip, deflate”,“Accept-Language”: “zh-CN,en-US;q=0.8,en;q=0.6”}
methodString默认为 GET,可以是:OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT
responseTypeString支持返回类型是 text,json,file,arraybuffer,默认会根据服务器返回 header 中的 Content-Type 确定返回类型,详见 success返回值
timeoutNumber超时时间,单位 ms,默认值为 7000
successFunction成功返回的回调函数
failFunction失败的回调函数,可能会因为权限失败
completeFunction结束的回调函数(调用成功、失败都会执行)
dataContent-Type说明
String不设置Content-Type 默认为 text/plain,data 值作为请求的 body
String任意 Typedata 值作为请求的 body
Object不设置Content-Type 默认为 application/x-www-form-urlencoded,data 按照 url 规则进行 encode 拼接作为请求的 body
Objectapplication/x-www-form-urlencodeddata 按照 url 规则进行 encode 拼接作为请求的 body
Objectapplication/x-www-form-urlencoded 之外的任意 type会将 data 转为字符串作为请求的 body
ArrayBuffer不设置Content-Type 默认为 application/octet-stream,data 值作为请求的 body
ArrayBuffer任意 Typedata 值作为请求的 body
参数名类型说明
codeInteger服务器状态 code
dataString/Object /ArrayBuffer参考 responseType与success中data关系 部分
headersObject服务器 response 的所有 header
responseType 与 success 中 data 关系:
Section titled “responseType 与 success 中 data 关系:”
responseTypedata说明
String服务器返回的 header 中 type 是 text/*或 application/json、application/javascript、application/xml,值是文本内容,否则是存储的临时文件的 uri,临时文件如果是图片或者视频内容,可以将图片设置到 image 或 video 控件上显示
textString返回普通文本
jsonObject返回 js 对象
fileString返回存储的临时文件的 uri
arraybufferArrayBuffer返回 ArrayBuffer 对象
fetch.fetch({
url: 'http://www.example.com',
responseType: 'text',
success: function (response) {
console.log(`the status code of the response: ${response.code}`)
console.log(`the data of the response: ${response.data}`)
console.log(`the headers of the response: ${JSON.stringify(response.headers)}`)
},
fail: function (data, code) {
console.log(`handling fail, errMsg = ${data}`)
console.log(`handling fail, errCode = ${code}`)
},
})