重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Ajax实现页面内跳转的代码怎么写

这篇“Ajax实现页面内跳转的代码怎么写”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Ajax实现页面内跳转的代码怎么写”文章吧。

成都创新互联公司主营庄浪网站建设的网络公司,主营网站建设方案,重庆App定制开发,庄浪h5微信小程序搭建,庄浪网站营销推广欢迎庄浪等地区企业咨询

代码如下:


    
        $(function(){        //作业面局部刷新,不做页面跳转的时候,推荐使用ajax
            $("#login").click(function(){                //点击按钮实现登录功能
                /* $.ajax({
                    type:'post', //type:请求方式,get,post
                    url:'login.action',   //要访问的后台地址
                    data:{
                        'uname':$("#uname").val(),
                        'pwd':$("#pwd").val()
                    },
                    success:function(result) {
                        if(result=='1') {
                            //1.welcome
                            location.href="welcome.jsp";
                        } else {
                            //2.login
                            $("#tip").show();
                        }
                    }
                }); */
                /* $.post('login.action',{
                    'uname':$("#uname").val(),
                    'pwd':$("#pwd").val()
                },function(result){
                    //回调函数:当后台成功响应结果时,会自动调用
                    if(result=='1') {
                        //1.welcome
                        location.href="welcome.jsp";
                    } else {
                        //2.login
                        $("#tip").show();
                    }
                }); */
                $.get('login.action?uname='+$("#uname").val()+'&pwd='+$("#pwd").val(),function(result){                    //回调函数:当后台成功响应结果时,会自动调用
                    if(result=='1') {                        //1.welcome
                        location.href="welcome.jsp";
                    } else {                        //2.login
                        $("#tip").show();
                    }
                });
            })
        })    

body之中的内容:

  
    
        
            
            
        
        
            
            
        
        
            
                
            
        
    
用户名:                              
密码:                              
    用户名或密码错误   

LoginServlet.java中的doPost()方法:

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String uname=request.getParameter("uname");
        String pwd=request.getParameter("pwd");
        System.out.println(uname);        //1.登录成功
        //2.登录失败
        if("admin".equals(uname)&&"123".equals(pwd)) {            //welcome
            out.print("1");
        } else {            //login
            out.print("2");
        }        out.flush();        out.close();
    }

以上就是关于“Ajax实现页面内跳转的代码怎么写”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注创新互联行业资讯频道。


标题名称:Ajax实现页面内跳转的代码怎么写
浏览地址:http://cqcxhl.cn/article/gigddp.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP