apicloud ajax html,使用APICloud编写优雅的HTML5代码《二》:ajax、文件上传

下面简单介绍各种类型请求的代码:

1、GET请求代码示例:

api.ajax({

url:'http://m.weather.com.cn/data/101010100.html'    //天气预报网站的WebService接口

},function(ret,err){

if (ret) {

api.alert({msg:JSON.stringify(ret)});

} else {

api.alert({msg:JSON.stringify(err)});

};

});

2、POST请求示例:

POST请求-Form表单提交:

api.ajax({

url: 'http://www.xxx.com/path/form',

method: 'post',

dataType: 'text',     //该参数若不传,则默认为json

data: {

values:{name: 'devlp', password: '123456'}       //键值对

}

},function(ret,err){

if (ret) {

api.alert({msg:JSON.stringify(ret)});

} else {

api.alert({msg:JSON.stringify(err)});

};

});

POST请求-单个/多个文件,文件组上传:

api.ajax({

url: 'http://www.xxx.com/path/upLoad',

method: 'post',

data: {

files:{myfile: 'filepath'}

// filepath来自ios或者Android的文件系统中的任意文件。可设置多个文件,甚至是文件数组,如files:{myfile: 'filepath', myfile1: 'filepath1'}或者files:{'myfile[]': ['filepath', 'filepath1']}等

}

},function(ret,err){

if (ret) {

api.alert({msg:JSON.stringify(ret)});

} else {

api.alert({msg:JSON.stringify(err)});

};

});

POST请求-提交二进制流:

api.ajax({

url: 'http://www.xxx.com/path/body',

method: 'post',

data: {

body:'textbits'

}

},function(ret,err){

if (ret) {

api.alert({msg:JSON.stringify(ret)});

} else {

api.alert({msg:JSON.stringify(err)});

};

});

POST请求-提交文件流:

api.ajax({

url: 'http://www.xxx.com/path/body',

method: 'post',

data: {

stream:'filepath'

// filepath来自ios或者Android的文件系统中的任意文件

}

},function(ret,err){

if (ret) {

api.alert({msg:JSON.stringify(ret)});

} else {

api.alert({msg:JSON.stringify(err)});

};

});

POST请求-Multipart-Data,文件和文本字段一起提交:

api.ajax({

url: 'http://www.xxx.com/path/multipart',

method: 'post',

data: {

values:{name: 'devlp', password: '123456'},

files:{file: 'fs://test.png'}

}

},function(ret,err){

if (ret) {

api.alert({msg:JSON.stringify(ret)});

} else {

api.alert({msg:JSON.stringify(err)});

};

});

POST请求-显示上传进度:

api.ajax({

url: 'http://www.xxx.com/path/multipart',

method: 'post',

report: true,

data: {

values:{name: 'devlp', password: '123456'},

files:{file: 'fs://test.png'}

}

},function(ret,err){

if(ret){

if(0 == ret.status){

//loading('进度:' + ret.progress);

}else{

api.alert({msg:'上传成功:\n' + JSON.stringify(ret)});

}

}else{

api.alert({msg:JSON.stringify(err)});

}

});

除此以外,put、delete、head等方式请求使用方式大同小异,在此不做详细叙述。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值