t1441332354
2018-06-29 13:36
采纳率: 71.8%
浏览 859

关于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条回答 默认 最新

  • 疾风雷颖 2018-06-30 00:43
    已采纳

    我认为可以做以下调整:

    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();
    
    打赏 评论
  • qq_34375658 2018-06-29 14:03

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

    打赏 评论
  • threenewbee 2018-06-29 15:03

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

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

    打赏 评论

相关推荐 更多相似问题