跨域异步请求

需要加载的模块

  1. const eeui = app.requireModule('eeui');

eeui.ajax

跨域请求

  • 支持自定义headers
  • 支持上传文件
  1. /**
  2. * @param params 详细参数
  3. * @param callback 回调事件
  4. */
  5. eeui.ajax({params}, callback(result))

params 参数说明

属性名 类型 必须 描述 默认值
url String 请求地址 -
name String - 请求名称,用于取消请求 -
method String - 请求类型,getpost get
dataType String - 返回数据类型,jsontext json
timeout Number - 请求超时时间(单位:毫秒) 15000
cache Number - 缓存时间,0不缓存(单位:毫秒) 0
headers Object - 请求头部headers -
data Object - 发送数据 -
files Object - 提交/上传文件 -
beforeAfter Boolen - 是否回调前(ready)后(complete)事件 false
progressCall Boolen - 是否回调上传进度事件 false

callback 回调result说明

  1. {
  2. status: 'success', //状态,详见:注①
  3. code: 200, //请求结果状态码
  4. headers: { ... }, //请求结果headers信息
  5. result: '...', //请求结果
  6. name: 'requestName', //请求名称
  7. url: 'http://....', //请求地址
  8. cache: false, //请求结果是否为缓存
  9. progress: { //上传进度信息
  10. current: 10240, // 当前进度大小
  11. total: 20480, // 总进度大小
  12. fraction: 0.5, // 进度比0~1
  13. }
  14. }

注①:

  • ready就绪
  • progress上传进度更新
  • success请求成功
  • error请求失败
  • complete请求结束

回调过程:ready -> [progress] -> (success | error) -> complete

:::warning 状态提示

  • readycomplete事件仅在beforeAfter=true时有回调

  • progress事件仅在上传文件progressCall=true时有回调

:::

简单示例

  1. //示例①
  2. eeui.ajax({
  3. url: 'http://....'
  4. }, function(result) {
  5. //......
  6. });
  7. //示例②
  8. eeui.ajax({
  9. url: 'http://....',
  10. method: 'post',
  11. headers: {
  12. token: 'x2eefhjb2h3rj'
  13. },
  14. data: {
  15. username: 'eeui'
  16. },
  17. files: {
  18. userimg: '/storage/sdcard/.....'
  19. }
  20. }, function(result) {
  21. //......
  22. });

eeui.ajaxCancel

取消跨域请求

  1. /**
  2. * @param name 请求名称(留空则取消所有请求)
  3. */
  4. eeui.ajaxCancel(name)

eeui.getCacheSizeAjax

获取跨域请求缓存

  1. /**
  2. * @param callback 回调事件,{size:123123},单位:字节B
  3. */
  4. eeui.getCacheSizeAjax(callback(result))

eeui.clearCacheAjax

清除跨域请求缓存

  1. eeui.clearCacheAjax()