mui ajax方法详解:

mui提供了mui.ajax,在此基础上有分装出mui.get()/mui.getJSON()/mui.post()三个方法。

mui.ajax( url [,settings] )

mui.ajax( url [,settings] )

url
Type: String
请求发送的目标地址 settings
Type: PlainObject
key/value格式的json对象,用来配置ajax请求参数,支持的完整参数参考如下mui.ajax([settings])方法

mui.ajax([settings])

settings
Type: PlainObject
key/value格式的json对象,用来配置ajax请求参数,支持的详细参数如下:
async
Type: Boolean
发送同步请求
crossDomain *5+ only
Type: Boolean
强制使用5+跨域
data
Type: PlainObject||String
发送到服务器的业务数据
dataType
Type: String
预期服务器返回的数据类型;如果不指定,mui将自动根据HTTP包的MIME头信息自动判断;支持设置的dataType可选值:
"xml": 返回XML文档
"html": 返回纯文本HTML信息;
"script": 返回纯文本JavaScript代码
"json": 返回JSON数据
"text": 返回纯文本字符串
error
Type: Functon(XMLHttpRequest xhr,String type,String errorThrown)
请求失败时触发的回调函数,该函数接收三个参数:
xhr:xhr实例对象
type:错误描述,可取值:"timeout", "error", "abort", "parsererror"、"null"
errorThrown:可捕获的异常对象
success
Type: Functon(Anything data,String textStatus,XMLHttpRequest xhr)
请求成功时触发的回调函数,该函数接收三个参数:
data:服务器返回的响应数据,类型可以是json对象、xml对象、字符串等;
textStatus:状态描述,默认值为'success'
xhr:xhr实例对象
timeout
Type: Number
请求超时时间(毫秒),默认值为0,表示永不超时;若超过设置的超时时间(非0的情况),依然未收到服务器响应,则触发error回调
type
Type: String
请求方式,目前仅支持'GET'和'POST',默认为'GET'方式
headers
Type: Json
指定HTTP请求的Header
headers:{'Content-Type':'application/json'}
processData
Type: Boolean
为了匹配默认的content-type("application/x-www-form-urlencoded"),
mui默认会将data参数中传入的非字符串类型的数据转变为key1=value&key2=value2格式的查询串;
如果业务需要,希望发送其它格式的数据(比如Document对象),可以设置processData为false

代码示例:如下为通过post方式向某服务器发送鉴权登录的代码片段

mui.ajax('http://server-name/login.php',{
data:{
username:'username',
password:'password'
},
dataType:'json',//服务器返回json格式数据
type:'post',//HTTP请求类型
timeout:10000,//超时时间设置为10秒;
headers:{'Content-Type':'application/json'},
success:function(data){
//服务器返回响应,根据响应结果,分析是否登录成功;
...
},
error:function(xhr,type,errorThrown){
//异常处理;
console.log(type);
}
});

mui.post()方法是对mui.ajax()的一个简化方法,直接使用POST请求方式向服务器发送数据、且不处理timeout和异常(若需处理异常及超时,请使用mui.ajax()方法),使用方法: mui.post(url[,data][,success][,dataType]),如上登录鉴权代码换成mui.post()后,代码更为简洁,如下:

mui.post('http://server-name/login.php',{
username:'username',
password:'password'
},function(data){
//服务器返回响应,根据响应结果,分析是否登录成功;
...
},'json'
);

mui.get()方法和mui.post()方法类似,只不过是直接使用GET请求方式向服务器发送数据、且不处理timeout和异常(若需处理异常及超时,请使用mui.ajax()方法),使用方法: mui.get(url[,data][,success][,dataType]),如下为获得某服务器新闻列表的代码片段,服务器以json格式返回数据列表

mui.get('http://server-name/list.php',{category:'news'},function(data){
//获得服务器响应
...
},'json'
);

如上mui.get()方法和如下mui.ajax()方法效果是一致的:

mui.ajax('http://server-name/list.php',{
data:{
category:'news'
},
dataType:'json',//服务器返回json格式数据
type:'get',//HTTP请求类型
success:function(data){
//获得服务器响应
...
}
});

mui.getJSON()方法是在mui.get()方法基础上的更进一步简化,限定返回json格式的数据,其它参数和mui.get()方法一致,使用方法: mui.get(url[,data][,success]),如上获得新闻列表的代码换成mui.getJSON()方法后,更为简洁,如下:

mui.getJSON('http://server-name/list.php',{category:'news'},function(data){
//获得服务器响应
...
}
);

以上内容获取为文档,详情请阅读文档。
http://dev.dcloud.net.cn/mui/ajax/

小结:

         <script type="text/javascript">
mui.init();
// plus加载完毕
mui.plusReady(function(){
document.getElementById('btn').addEventListener('tap',function(){
// mui.ajax GET方法
mui.ajax({
type: 'GET',
url: 'http://demo.hcoder.net/index.php',
success: function(msg){
mui.toast(msg);
},
error: function(xhr, type, errorThrown){
// xhr:xhr实例对象 type:错误描述 errorThrown:可捕获的异常对象
mui.toast(type);
}
}); // mui.ajax POST方法
mui.ajax({
type: 'POST',
data: {'name':'hcoder', 'age': 18},
dataType: 'json',
url: 'http://demo.hcoder.net/index.php',
success: function(msg){
mui.toast(JSON.stringify(msg));
},
error: function(xhr, type, errorThrown){
mui.toast(type);
}
}); // mui.post方法 传入的是参数而不再是对象
mui.post(
'json',
'http://demo.hcoder.net/index.php',
{'name':'hcoder', 'age': 18},
function(msg){
console.log(JSON.stringify(msg));\
mui.toast(msg.name);
}
); // mui.get方法 没有data元素
mui.get(
'json',
'http://demo.hcoder.net/index.php',
function(msg){
console.log(JSON.stringify(msg));
}
); // mui.getJSON方法
mui.getJSON(
"http://demo.hcoder.net/index.php",
function(msg){
console.log(JSON.stringify(msg));
mui.toast(msg.name);
}
);
});
});
</script>

  

mui ajax方法的更多相关文章

  1. Mui --- app与服务器之间的交互原理、mui ajax使用

    1.APP与服务器之间的交互原理 app端(客户端)与服务端的交互其实理解起来和容易,客户端想服务器端发送请求,服务器端进行数据运算后返回最终结果.结果可以是多种格式: 1.text 文本格式 2.x ...

  2. mui初级入门教程(三)— html5+ XMLHttpRequest 与mui ajax用法详解

    文章来源:小青年原创发布时间:2016-05-29关键词:mui,html5+,XMLHttpRequest,ajax,懒加载转载需标注本文原始地址: http://zhaomenghuan.gith ...

  3. Hbuilder app开发,使用mui.ajax和服务器交互,后台获取不到值,显示null的解决方法

    先上一个能用的js代码: function login() { var uname=document.getElementById("username").value.trim() ...

  4. mui ajax 应用的跨域问题

    1.首先在mui.ajax的error函数里出现: “syntaxerror unexpected token <” 这样的错误,那么在 mui.ajax中的type写成  JSONP ,后台需 ...

  5. mui ajax

    <!doctype html><html> <head> <meta charset="UTF-8"> <title>直 ...

  6. $.ajax()方法详解

    jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(p ...

  7. Mui.ajax请求服务器正确返回json数据格式

    ajax: mui.ajax('http://server-name/login.php',{ data:{ username:'username', password:'password' }, d ...

  8. 重写jquery的ajax方法

    //首先备份下jquery的ajax方法 var _ajax=$.ajax; //重写jquery的ajax方法 $.ajax=function(opt){ //备份opt中error和success ...

  9. ajax方法总结

    ajax方法总结 1.原生ajax get请求和post请求区别:黄色小三角 以get请求为例,输出结果如下: 2.jquery中的ajax 列了常用的6个方法: 3.状态说明 readystate: ...

随机推荐

  1. python实现RabbitMQ同步跟异步消费模型

    1,消息推送类 import pika # 同步消息推送类 class RabbitPublisher(object): # 传入RabbitMQ的ip,用户名,密码,实例化一个管道 def __in ...

  2. JavaScript的基础学习(一)

    一.JavaScript概述 JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEase ...

  3. 洛谷——P1692 部落卫队

    题目描述 原始部落byteland中的居民们为了争夺有限的资源,经常发生冲突.几乎每个居民都有他的仇敌.部落酋长为了组织一支保卫部落的队伍,希望从部落的居民中选出最多的居民入伍,并保证队伍中任何2 个 ...

  4. POJ 3662 Telephone Lines (分层图)

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6785   Accepted: 2498 D ...

  5. Flask实战第62天:帖子详情页布局

    在templates/front/下创建详情页面front_pdetail.html 编辑front.views.py创建详情页的视图函数 from flask import abort ... @b ...

  6. eclipse汉化 adt汉化

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha

  7. 【计算几何】【推导】【补集转化】AtCoder Regular Contest 082 E - ConvexScore

    题意:平面上给你N个点.对于一个“凸多边形点集”(凸多边形点集被定义为一个其所有点恰好能形成凸多边形的点集)而言,其对答案的贡献是2^(N个点内在该凸多边形点集形成的凸包内的点数 - 该凸多边形点集的 ...

  8. Android应用程序的解析

    一: 文件架构 二: 图片,语音资源的使用 图片的两种使用方法: 第一种: 使用imageView控件 <ImageView android:id="@+id/imageView1&q ...

  9. 定时任务框架-quartz 时间配置

    quartz定时任务时间设置: 这些星号由左到右按顺序代表 : * * * * * * * 格式: [秒] [分] [小时] [日] [月] [周] [年] * 表示所有值. 例如:在分的字段上设置 ...

  10. golang解析json格式 -- 全

    项目中客户端和服务端的交互数据部分为json,因此在服务端就得解析,复杂的json解析起来其实还是挺费劲的. 交互的数据类似如下格式: {"sn":1,"ls" ...