var _url = '服务器IP地址或接口地址,不能用localhost';
var logininfo = {
username: 'username', //POST方法提交的用户名
password: 'password' //POST方法提交的密码
}
mui.ajax(_url, {
data: logininfo,
dataType: 'json', //要求服务器返回json格式数据
type: 'post', //HTTP请求类型,要和服务端对应,要么GET,要么POST
timeout: 10000, //超时时间设置为10秒;
beforeSend: function() { //发送之前,可以打一下看看提交的参数(如果是变量)
console.log('beforesend!' + JSON.stringify(logininfo))
plus.nativeUI.showWaiting(); //关闭转圈
},
success: function(data) {
console.log(JSON.stringify(data)) //成功,则打一下返回的数据
//登陆成功与否可以根据返回的data里自己组装的code、success、err=0之类的判断
//在这里关闭login页面,open登陆之后的index页面
},
error: function(xhr, type, errorThrown) { //失败,打一下失败的类型,主要用于调试和用户体验
console.log('AJAX:|T:' + type + '|H:' + xhr.responseText);
if(type == 'timeout') {
mui.toast("请求超时:请检查网络")
} else {
mui.toast('请求失败:' + type + '\n err:' + errorThrown);
}
},
complete: function() {
console.log('userLogin:关闭转圈')
plus.nativeUI.closeWaiting(); //关闭转圈
}
}); //ajax end
4 个回复
最佳回复
好冷 - 诚接外包,QQ:23380891
q***@163.com
我用mui.ajax 服务器报跨域错误,jquery的就可以。
是否用mui.ajax的位置不对,一定要放在 plusready里么?
好冷 - 诚接外包,QQ:23380891
mui.ajax在mui.ready就能用了,不一定非要等到plusready
f***@163.com
跨域名需要授权,加个http头