使用APICloud编写优雅的HTML5代码《二》:ajax、文件上传
来源:互联网 发布:软件部门职责说明书 编辑:程序博客网 时间:2024/06/05 09:45
摘要: 对于ajax这个API而言,它实际上是有非常多的参数可供开发者选择的,以此满足各种特殊的需求,包括:url、method、cache、timeout、dataType、charset、headers、report、returnAll、data等等,用户可以根据实际情况,结合使用这些参数,来完成非常多的需求,如通常的访问WebService接口获取数据(GET)、提交表单(POST)、上传文件、下载文件等。一个简单的API却包含了非常强大的功能。
下面简单介绍各种类型请求的代码:
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
- 使用APICloud编写优雅的HTML5代码《二》:ajax、文件上传
- 使用APICloud编写优雅的HTML5代码《一》:下拉刷新、上拉加载更多
- html5 ajax文件上传
- html5 ajax文件上传
- html5 ajax文件上传
- 使用Guava编写优雅代码
- html5 ajax文件上传的进度条实现
- HTML5的上传文件AJAX方式
- APICloud改项目代码后,使用svn上传及下载
- APICloud的ajax请求
- MVC5:使用Ajax和HTML5实现文件上传功能
- html5里使用ajax提交含有上传文件的form表单
- 使用html5技术实现文件的上传
- 教你编写优雅的JavaScript代码
- 如何编写简单优雅的代码
- 《编写优雅的前端业务代码》听后感
- 编写高效且优雅的 Python 代码
- 编写高效且优雅的 Python 代码
- Android系统高通平台Kernel Watchdog
- vue中如何实现样式之间的切换
- 异常:javax.el.PropertyNotFoundException: Property 'xxx' not found on type java.lang.String
- python字符串分割
- 确定比赛名次--topo
- 使用APICloud编写优雅的HTML5代码《二》:ajax、文件上传
- DNS服务器【Linux-shell】
- 首次使用maven的配置过程
- linux下的一些常见命令学习
- 测试8.5
- python中二进制,八进制,十六进制的表现形式
- JVM &JMM && 类加载
- laravel的原生子查询,用来统计3张表的相关数据
- bootstrap系列之十按钮组的使用