笔记 · · 1325 阅读

ajax通用格式

ajax
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(){[removed].href=""+data.url+""},1000);

                }

                if(data.msg == null){

                    layer.msg('服务器错误');

                    setTimeout(function(){[removed].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")

    }

    })
0 评论

评论

还没有评论,来说两句吧