ajax请求成功前loading
1.jquery方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ajax加载loading图标</title>
</head>
<body>
<form id="test-form" action="test.php" method="post">
<input type="text" name="username">
</form>
<button id="btn">提交</button>
<div id="loading"></div>
<div id="res"></div>
</body>
<script>
$('#btn').click(function(){
$.ajax({
url:$('#test-form').attr('action'), //发送后台的url
type:'post',
data:$('#test-form').serialize(), //序列化表单内容
dataType:'text', //后台返回的数据类型
timeout:15000, //超时时间
beforeSend:function(XMLHttpRequest){
$("#loading").html("<img src='./images/loading.gif' />"); //在后台返回success之前显示loading图标
},
success:function(data){ //data为后台返回的数据
$("#loading").empty(); //ajax返回成功,清除loading图标
$('#res').html('ajax请求成功!');
}
});
});
</script>
</html>
2.js方式
xmlhttp.onreadystatechange = function(){
var loadingdiv = document.getelementbyid("loading");
loadingdiv.innerhtml = "loading...";
loadingdiv.style.right = "0";
loadingdiv.style.top = "0";
loadingdiv.style.display = "block";
//load completed
if(xmlhttp.readystate == 4) {
loadingdiv.style.display = "none";
loadingdiv.innerhtml = "";
if(xmlhttp.status == 200) {
var str = xmlhttp.responsetext;
/* do something here ! */
}
else
alert("error!" + "nstatus code is: " + xmlhttp.status + "nstatus text is: " + xmlhttp.statustext);
}
}
最新文章
- 产生某个区间的随机整数 int #Java
- 材价看板(2)- 运行两周的kanban,改进的起点
- iOS开发——运行时OC篇&;使用运行时获取系统的属性:使用自己的手势修改系统自带的手势
- Reservoir Sampling - 蓄水池抽样
- H - Frequent values
- jni 之helloworld
- Xcode7 低版本iOS系统上下有黑边的问题
- windows mysql 中文乱码和中文录入提示太大错误的解决方法
- ZOJ-2587-Unique Attack(最小割的唯一性)
- Sicily-1438
- RedHat 7 常用命令总结
- 201521123020《Java程序设计》第2周学习总结
- Bootstrap-datepicker3官方文档中文翻译---概述(原版翻译 http://bootstrap-datepicker.readthedocs.io/en/latest/index.html)
- [物理学与PDEs]第4章习题2 反应力学方程组形式的化约 - 能量守恒方程
- ip xfrm命令是做什么的?
- Eclipse中避免修改后台代码后手动install和重启
- python 列表常用操作(二)
- Linux 文件系统结构、磁盘的管理
- hadoop系列 第二坑: hive hbase关联表问题
- Pupet自动化管理环境部署记录