一直使用jquery的ajax 来处理异步的问题,其相当的方便,最近因工作项目需求,需要处理异步提交的数据源返回来的各种状态,现分享给大家学习学习,不多说,直接上代码:
<script src='jquery.js'></script> <script> $(function(){ $("#starts").click(function(){ $.ajax({ type:'POST', url:'ajax.test.php', data:"username=2", dataType:'text', cache:false, beforeSend: function(XMLHttpRequest){ $("#loading").show(); }, success:function(msg){ alert(msg); }, complete: function(XMLHttpRequest, textStatus){ $("#loading").hide(); }, error:function(data){ if(data.status=="404"){ alert('请求地址出错!'); } else if(data.status=="302"){ alert('连接网页出错'); } else if(data.status=="timeout"){ alert("请求超时!"); }else{ alert('请求未响应!请检查网络或VPN连接'); } } }); }); }); </script> <span id="starts" style="cursor: pointer;">start</span>||||||||||||| <span id="loading" style="display:none;">loading......</span>