关于ajax后台传值的一些问题

这是前端用ajax向后台传值的部分代码

 <script type="text/javascript">
    function checkselect(){
        $.ajax({
        async:false,
        cache:false,
        type:'post',
        url:'ChangeServlet',
        data:{username:Username,password:Password,changepass:ChangePass},
        success:function(data){
            if(data == 1){
                alert("密码修改成功!");
            }else if(data == 0){
                alert("账号与原密码不匹配,请重新输入!");
            }
        },
            error : function() {
                alert("网络故障!");
            }   
        });
    }
</script>

后台实现功能代码如下

            String sql = "select * from LoginUser where UserName = '"+username+"' and LoginPwd = '"+pasword+"'";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                String isExit = "update LoginUser set LoginPwd = ? where UserName = '"+username+"'";
                PreparedStatement ps2 = conn.prepareStatement(isExit);
                ps2.setString(1,changepass);
                ps2.executeUpdate();
                ps2.close();
                conn.close();

            }else{

            }
            rs.close();
            ps.close();
            conn.close();

后台是servlet,在“conn.close()”之后判定data=1,在else里面判定data=0,并将它们的值传回前台应该怎么写?

3个回答

我认为可以做以下调整:

PrintWriter out = response.getWriter();

                    String sql = "select * from LoginUser where UserName = '"+username+"' and LoginPwd = '"+pasword+"'";
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        if(rs.next()){
            String isExit = "update LoginUser set LoginPwd = ? where UserName = '"+username+"'";
            PreparedStatement ps2 = conn.prepareStatement(isExit);
            ps2.setString(1,changepass);
            ps2.executeUpdate();
            ps2.close();
            conn.close();

out.println(“1”);

        }else{

out.println(“0”);

        }
        rs.close();
        ps.close();
        conn.close();

ajax要json数据 返回一个json格式的字符串 前台就收就可以了 就比如 response.getWriter().Writer("flag:参数");

用JSONObject,遍历上面的rs,jsonobj.put进去

代码参考:https://www.cnblogs.com/boguse/p/6769092.html?utm_source=itdadao&utm_medium=referral

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问