现在的位置: 首页 > 综合 > 正文

jquery 中ajax的所有状态

2012年11月11日 ⁄ 综合 ⁄ 共 743字 ⁄ 字号 评论关闭

一直使用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>

抱歉!评论已关闭.