Ajax与Java交互简单用例


Ajax与Java交互简单用例


第一次写博客呀,好激动,现在是凌晨一点五十。开始正题
最近在写一个小的项目,需要使用Ajax来将数据从前台页面传送到后台,于是学习了一些Ajax便开始写了,下面是一些总结:
**Ajax带着数据请求到服务器后,又会带着数据回到当前页面,所以不能使用servlet进行请求的转发**
**AJAX 是与服务器交换数据并更新部分网页的艺术,在**不重新加载整个页面**的情况下,通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新。这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新。和服务器进行**异步通信**

传统的网页(不使用 AJAX)如果需要更新内容,必需重载整个网页面。**
## 1.页面js代码 ##

function onSubmit(){
                    var form_data=$('#form表单id').serialize();//获得表单中所有数据
                    var option={
                            url:'/questionnaire/UserController',
                            type:'POST',
                            dataType:'json',
                            data:form_data,
                            success:function(result){
                                if(result){
                                    window.location.href="页面url";
                                }else{
                                    alert("登陆失败,重新输入");
                                }



                            }
                    };
                    $.ajax(option);    
                }
    form id="account_info_mobile" action="" method="post" style="display: block;">
                            <input type="hidden" name="mathed" value="userLogin">
                        <ul>
                            <li style="margin-top: 30px;">
                                <input type="text" id="mobile" name="mobile" class="requested" placeholder="手机号/邮箱" onblur="onBlur(id)"/>

                            </li>
                            <div class="font" style="margin-top:0px;margin-bottom: 7px;">
                            <span id="userText" class="font1"></span>
                            </div>
                            <li>
                                <input type="password" id="psw" name="password" class="requested" placeholder="密码" onblur="onBlur(id)"/>
                            </li>

                            <div class="fontt" style="margin-top:3px;">
                            <span id="psdText" class="font2"></span>
                            </div>
                        </ul>
                        </form>
                </div>
                <a href="findPsw.jsp" style="float: right;">忘记密码</a>
                <button onclick="onSubmit()" type="button" id="submit" class="btn btn-info" style="margin-left: 50px;width: 140px;height: 50px;text-align: center;font-size: 20px;margin-top: 10px;letter-spacing: 5px;" disabled="disabled">登录</button>

点击按钮触发事件,想url所指向后台地址发起post请求,
2. java后台代码,使用阿里fastjson处理json数据,需要先导入.alibaba.fastjson架包
获得页面数据 request.getParameter("name属性值");

response.setContentType("application/json;charset=utf-8");
        boolean b=false;
        String res=JSON.toJSONString(b);
        if(pwd!=null&&repwd!=null&&pwd.equals(repwd)){
            User u=new User();
            u.setUserPwd(pwd);
            System.out.println("session中userId:"+request.getSession().getAttribute("userId"));
            u.setUserId((String)request.getSession().getAttribute("userId"));
            ResultDao<Boolean> result=userService.resetPsw(u);
            if(result.isSuccess()){
                request.getSession().setAttribute("userId", u.getUserId());
                b=true;
                res=JSON.toJSONString(b);
            }
        }
        PrintWriter out = response.getWriter();
        out.write(res);
        out.flush();
        out.close();

用例到此结束,很简单的一个用例

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值