ajax通用格式

发表于

一个简单的例子,通过绑定点击事件触发ajax数据提交

 $(“.subBtn”).on(“click”,function(){

        if(subMit()){
            var old_mobile = $(‘#old_mobile’).val();
            var mobile = $(‘#phone’).val();
            var codesms = $(‘#code’).val();
            $.ajax({
                url: “{:U(‘user/mobile_validate’)}”,
                type: ‘post’,
                dataType: ‘json’,
                data: {mobile: mobile,old_mobile:old_mobile,codesms: codesms},
                beforeSend: function () {
                    layer.msg(‘提交数据中…’);
                    // 禁用按钮防止重复提交
                    $(“#submit”).attr({ disabled: “disabled” });
                },
                success:function(data){
                    console.log(data);
                    if(data.code == 300){
                        layer.msg(data.msg);
                        setTimeout(function(){window.location.href=””+data.url+””},1000);
                    }
                    if(data.msg == null){
                        layer.msg(‘服务器错误’);
                        setTimeout(function(){window.location.href=””;},1000);
                        return false;
                    }
                    layer.msg(data.msg);
                },
                complete: function () {
                    // layer.msg(‘获取数据成功’);
                    $(“#submit”).removeAttr(“disabled”);
                },
                error:function(data){
                    console.info(“error: ” + data.responseText);
                },
            })
            // window.open(“registerFinish.html”,”_parent”)
        }

    })

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注