ajax与后台交互案例

BBS项目

		//BBS项目,注册页面ajax请求
        // 1.实现照片预览 
    $("#up_myhead").change(function () {
        // 获取input选择的文件
        let file_obj = $(this)[0].files[0];
        // 为该图片准备一个文件阅读器
        let read = new FileReader();
        // Starts reading the contents of the specified Blob, once finished, the result attribute contains a data: URL representing the file's data.
        // asynchronously read the contents of files
        read.readAsDataURL(file_obj);
        read.onload = function (event) {
            // 把获取到的传给img
            document.querySelector("#head_img").src = read.result
        }
    });
	// ajax提交注册
    $("#btn_submit").click(function () {
        // 生成提交对象
        let form_data = new FormData();
        let sub = $("#form_reg").serializeArray();
        $.each(sub, function (index, val) {
            form_data.append(val.name, val.value)
        });
        form_data.append("myfile", $("#up_myhead")[0].files[0]);
        console.log('留空1');
        $.ajax({
            url: "/register/",
            type: "post",
            contentType: false, //告诉jQuery不要去设置Content-Type请求头
            processData: false, //告诉jQuery不要去处理发送的数据
            data: form_data,
            success: function (data) {
                if (data.status == 100) {
                    window.location.href = '/login/'
                } else {
                    $.each(data.msg, function (index, val) {
                        $("#id_" + index).next().text(val).parent().addClass("has-error");
                        // 提示两次密码不一致
                        if (index == "__all__") {
                            $("#id_re_pwd").next().text(val).parent().addClass("has-error");
                        }
                    });
                    // 三秒后清除错误提示和error效果
                    setTimeout(function () {
                        let form = $(".form-group");
                        form.removeClass("has-error").children("span").empty();
                        form.children("input").val("");
                    }, 3000)
                }
            }
        })
    });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值