关于jquery ajax

关于jquery ajax

我想使用ajax,来获取当前账号有没有被创建,这是我servlet中的方法:

public void checkAccount(HttpServletRequest request,HttpServletResponse response) throws IOException {
    String account = request.getParameter("account");
    Staff staff = staffService.getByAccount(account);
    JSONObject jsonObject = null;
    if (null != staff){
        //数据库已存在该账号,则返回信息给ajax
        jsonObject = new JSONObject("{accountFlag:2}");
    }else {
        //数据库未存在该账号
        jsonObject = new JSONObject("{accountFlag:1}");
    }
    System.out.println("调用");
    System.out.println(jsonObject.toString().getBytes("utf-8"));
    response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
}

这是我jsp的代码

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=8" >
    <title>添加员工</title>
    <link rel="stylesheet" type="text/css" href="../css/reset.css"/>
    <link rel="stylesheet" type="text/css" href="../css/common.css"/>
    <link rel="stylesheet" type="text/css" href="../css/thems.css">
    <script type="text/javascript" src="../js/jquery-1.8.3.min.js"></script>
    <script type="text/javascript">
        $(function(){
            //自适应屏幕宽度
            window.onresize=function(){ location=location };

            var main_h = $(window).height();
            $('.hy_list').css('height',main_h-45+'px');

            var main_w = $(window).width();
            $('.xjhy').css('width',main_w-40+'px');

        });
    </script>
</head>
<script type="text/javascript">
    function get() {
        //点击输入账号时,触发ajax事件
        $.ajax({
            url:"checkAccount.do",
            type:"post",
            data:{
                account:$("input[name=account]").val()
            },
            dateType:"json",
            success:function (result) {
                var flag = result.flag;
                if (flag == 2){
                    //登录失败则提示用户
                    // $(".tip").text("该账号已被注册");
                    document.getElementById("tip").innerHTML="该账号已被注册";
                    alert("该账号已被注册");
                }else if (flag == 1) {
                    document.getElementById("tip").innerHTML="该账号未被注册";
                }else {
                    document.getElementById("tip").innerHTML="测试";
                }
            }
        });
    }
</script>
<body onLoad="Resize();">
<div id="right_ctn">
    <div class="right_m">
        <div class="hy_list">
            <div class="box_t">
                <span class="name">添加员工</span>
            </div>
            <div class="space_hx">&nbsp;</div>
            <form action="add.do" method="post" name="addForm">
                <div class="xjhy">
                    <!--高级配置-->
                    <ul class="hypz gjpz clearfix">
                        <li class="clearfix">
                            <span class="title">姓名:</span>
                            <div class="li_r">
                                <input class="chang" name="name" type="text">
                            </div>
                        </li>
                        <li class="clearfix">
                            <span class="title">部门:</span>
                            <div class="li_r">
                                <select name="did" >
                                    <c:forEach items="${DLIST}" var="dep">
                                        <option value="${dep.id}">${dep.name}</option>
                                    </c:forEach>
                                </select>
                            </div>
                            <span class="tip"></span>
                        </li>
                        <li class="clearfix">
                            <span class="title">账号:</span>
                            <div class="li_r">
                                <input class="chang" name="account" type="text" onchange="get()">
                            </div>
                            <span class="tip" id="tip"></span>
                        </li>
                        <li class="clearfix">
                            <span class="title">密码:</span>
                            <div class="li_r">
                                <input class="chang" name="password" type="password">
                            </div>
                        </li>
                        <li class="clearfix">
                            <span class="title">性别:</span>
                            <div class="li_r">
                                <span class="radio">
                                <input checked="checked" name="sex" type="radio" value="男" >
                                <em>男</em>
                                </span>
                                <span class="radio">
                                <input  name="sex" type="radio" value="女">
                                <em>女</em>
                                </span>
                            </div>
                        </li>
                        <li class="clearfix">
                            <span class="title">备注:</span>
                            <div class="li_r">
                                <input class="chang" name="info" type="text">
                            </div>
                        </li>
                        <li class="tj_btn">
                            <a href="javascript:history.go(-1);" class="back">  返回</a>
                            <a href="javascript:addForm.submit();">保存</a>
                        </li>
                    </ul>
                    <!--高级配置-->
                </div>
            </form>
        </div>
    </div>
</div>
</body>
</html>

为什么我jsp的success无法获取正确的flag值?

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

5回答
提问者 乐高战士 2018-10-16 10:38:29
function checkAccount() {
    //点击输入账号时,触发ajax事件
    $.ajax({
        url:"checkAccount.do",
        type:"post",
        data:{
            account:$("input[name=account]").val()
        },
        dateType:"json",
        success:function (result) {
            var result = eval("("+result+")");
            var account = document.getElementById("account").value;
            //var account =  addForm.account.value;也可以
            var accountFlag = result.accountFlag;
            if (account == "") {
                document.getElementById("accountTip").innerHTML="请输入账号";
            }else if (accountFlag == 1){
                document.getElementById("accountTip").innerHTML="该账号已被注册";
            }else if (accountFlag == 2) {
                document.getElementById("accountTip").innerHTML=" ";
            }
        }
    });
}
public void checkAccount(HttpServletRequest request,HttpServletResponse response) throws IOException {
    String account = request.getParameter("account");
    Staff staff = staffService.getByAccount(account);
    JSONObject accountJson = null;

    if (null != staff){
        //数据库已存在该账号,则返回信息给ajax
        accountJson = new JSONObject("{accountFlag:1}");
    }else if (null == staff){
        //数据库未存在该账号
        accountJson = new JSONObject("{accountFlag:2}");
    }
    response.setContentType("text/text");
    response.getOutputStream().write(accountJson.toString().getBytes("utf-8"));
}

这个方法也能被调用,不知道为什么checkDelete方法不能被调用

  • 你好同学。checkDelete是方法名,你需要在html代码中调用这个方法,譬如在html页面中添加一个按钮<button name="button" value="button" onclick="checkDelete()"/>通过点击这个按钮来触发checkDelete()方法,祝学习愉快~
    2018-10-16 13:50:11
chrismorgen 2018-10-16 10:05:06

使用Ajax传递字符串可以使用下图方式进行传递,另外一个问题建议你检查一下在你的代码中是否调用了checkDelete方法,祝学习愉快~

http://img1.sycdn.imooc.com//climg/5bc545790001b2ee03920174.jpg

  • 提问者 乐高战士 #1
    我想知道怎么检查是否调用了checkDelete方法。。。我觉得他没有被调用,我写的别的ajax代码都被调用了。。。您看一下
    2018-10-16 10:37:31
提问者 乐高战士 2018-10-15 23:03:10

页面完全没有触发ajax事件,没什么啊。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

http://img1.sycdn.imooc.com//climg/5bc4ac2b0001ca2e19090455.jpg

提问者 乐高战士 2018-10-15 21:29:52
<script type="text/javascript" language="JavaScript">
    function checkDelete() {
        var flag = confirm("是否确定要删除该员工?");
        if (flag == true){
            //触发ajax事件
            $.ajax({
                url:"checkDelete.do",
                type:"post",
                data:{
                    deleteFlag:1
                },
                dateType:"json",
                success: function (result) {
                    alert("删除成功");
                }
            });
        }else{
            this.location.reload();
        }
    }
</script>
public void checkDelete(HttpServletRequest request,HttpServletResponse response) {
    String deleteFlagStr = request.getParameter("deleteFlag");
    Integer deleteFlag = Integer.parseInt(deleteFlagStr);
    System.out.println(deleteFlagStr);

}


chrismorgen 2018-10-15 11:07:12

建议将 var flag = result.flag;改为 var flag = result.accountFlag;试试,如果我的建议解决了你的问题,请采纳,祝学习愉快~

  • 提问者 乐高战士 #1
    嗯就是这样,谢谢,还有,我想问一下,我如果想传递string参数给后台,ajax代码要怎么写,我代码写到回答那里了
    2018-10-15 21:29:19
  • 提问者 乐高战士 #2
    它怎么都进不了ajax方法
    2018-10-15 22:08:30
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
SSM主流框架入门与综合项目实战2018版
  • 参与学习           人
  • 提交作业       205    份
  • 解答问题       4317    个

Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师