ajax请求
Ajax说明:
mui框架基于htm5plus的XMLHttpRequest,封装了常用的Ajax函数,支持GET、POST请求方式,支持返回json、xml、html、text、script数据类型; 本着极简的设计原则,mui提供了mui.ajax方法,并在mui.ajax方法基础上,进一步简化出最常用的mui.get()、mui.getJSON()、mui.post()三个方法。
mui.ajax(url[,settings])方法
参数类型和说明如下:
url:
Type: String 说明:请求发送的目标地址
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:
说明:请求失败时触发的回调函数,该函数接收三个参数:
xhr:xhr实例对象
type:错误描述,可取值:"timeout", "error", "abort", "parsererror"、"null"
errorThrown:可捕获的异常对象
Success:
说明:请求成功时触发的回调函数,该函数接收三个参数:
data:服务器返回的响应数据,类型可以是json对象、xml对象、字符串等;
textStatus:状态描述,默认值为'success'
xhr:xhr实例对象
Timeout:
Type: Number
说明:请求超时时间(毫秒),默认值为0,表示永不超时;若超过设置的超时时间(非0的情况),依然未收到服务器响应,则触发error回调
Type:
Type: String
说明:请求方式,目前仅支持'GET'和'POST',默认为'GET'方式
Headers:
Type: Json
说明:指定HTTP请求的Header
代码示例:如下为通过post方式向某服务器发送鉴权登录的代码片段
$.ajax(strurl + 'SecurityAlgorithm/DynamicCallDll/DebugRun',{
//向后台传参数
data: {
"SupplierID":SupplierID,
"ArithmeticID":ArithmeticID,
"T":3
},
type: 'get',//HTTP请求类型
datatype:'json',//服务器返回json格式数据
timeout:300,//通过timeout属性,设置超时时间
success: function (data){
console.log(data)
Ajax(SupplierID, ArithmeticID);
},
error:function(xhr,type,errorThrown){
if(type == 'error') {
mui.toast("请求超时")
}
},
})