AJAX

2019-05-05  本文已影响0人  金石_832e

直接上代码

<script src="${ pageContext.request.contextPath }/boot/js/jquery.js"></script>
    <script>
        $(function () {
            $('#checkname').on('blur', function () {
                $.ajax({
                    url: "${pageContext.request.contextPath}/user/checkregname",
                    data: {
                        name: $('input[name = username]').val(),
                    },
                    type: "post",
                    dataType: "json",
                    success: function (res) {
                        if (res == 1) {
                            $('#msg').css('color', 'red').html('用户名已存在!');
                            $('button').attr('disabled', 'disabled');

                        } else {
                            $('#msg').css('color', 'green').html('该名称可用!');
                            $('button').removeAttr('disabled', 'disabled');
                        }
                    },
                    error: function () {
                        alert("服务器无响应");
                    },
                    async: true
                })
            })
            $('button').on('click', function () {
                if ($('input[name=password]').val() == '') {
                    $('#pass_msg').css('color', 'red').html('请输入密码');
                    $('button').attr('disabled', 'disabled');
                }
            })
            $('input[name=password]').on('focus', function () {
                $('button').removeAttr('disabled', 'disabled');
            })
        })
    </script>

url:填写要发送到的servlet映射,上面写法是绝对路径。
data:设置传给servlet的参数。
type:设置请求方式。
dataType:设置返回对象类型。目前为json对象,可以使xml或其他格式的数据。
success: function (res){}:如果请求成功,进行一系列操作。
error: function () :如果请求不通过,进行一系列操作。
async:是否异步处理。true或false

上一篇 下一篇

猜你喜欢

热点阅读