|
我们使用jquery ajax请求数据时偶尔会遇到网络错误,系统就会弹出一个大大的Justep 230109 对话框,实在太不友好了,解决办法以就是使用原生的XMLHttpRequest();
function ajaxRequest(url, sendParams, type, callback){
var self = this;
var Url = url+"?"+ $.param(sendParams);
var xmlhttp = new XMLHttpRequest();
xmlhttp.timeout = 10000; //设置xhr请求的超时时间 10秒
xmlhttp.open(type, Url, true);
xmlhttp.onload = function(event) { // 提交成功,处理事项
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if (typeof (callback) === "function") {
callback.call(self, eval('(' + xmlhttp.responseText + ')'));
}
}
};
xmlhttp.onerror = function(event) { //错误信息
userAlert('Error: The Network unavaible') //这里是我自定义的对话框userAlert()
};
xmlhttp.ontimeout = function(event) { //错误信息
userAlert('Error: Timeout');
};
xmlhttp.upload.onprogress = function(event) { //数据提交进度,
if (event.lengthComputable) {
var completedPercent = Math.round((event.loaded / event.total) * 100);
}
};
// 发送数据
xmlhttp.send(null);
}
|
|