Be_Proud
Be_Proud
2016-02-02 13:38
采纳率: 45.5%
浏览 1.3k

关于 jquery实现Ajax 但是没有效果

 servlet:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println(1);

        List<String> userNames = Arrays.asList("AAA","BBB","CCC");
        String userName = request.getParameter("userName");
        String result = null;

        if(userNames.contains(userName)){
            System.out.println(2);
            result = "<font color='red'>该用户名已被使用</font>";
        }else {
            result = "<font color='green'>该用户名可以使用</font>";
        }
        System.out.println(3);
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().print(result);

    }







index.jsp
<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery-1.11.3.js/"></script>
 <script type="text/javascript">

    $(function(){
        $(":input[name='username']").change(function(){

            var val = $(this).val();
            val = $.trim(val);
            if(val!=""){
                var url = "${pageContext.request.contextPath}/login";
                var args = {"userName":val,"time":new Date()};
                $.post(url,args,function(data){
                    $("#message").html(data);
                });
            }
        });
    });
 </script>
</head>
<body>
    <form action=""  method="post">
        UserName:<input type="text" name="username" />
        <br/>
        <div id="message"></div>
        <br/>
        <input type="submit" value="Submit" />
    </form>
</body>
</html>

xml:


    <servlet>
        <description>
        </description>
        <display-name>UserNameServlet</display-name>
        <servlet-name>UserNameServlet</servlet-name>
        <servlet-class>com.servlets.UserNameServlet</servlet-class>
    </servlet>
        <servlet-mapping>
        <servlet-name>UserNameServlet</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • showbo
    已采纳

    怎么多了个/在最后?手误还是什么?如果有/在后面是目录了,应该是这个问题导致jquery加载不进去,最好用$.ajax配置error看是否执行到error,$.post只有200状态才会执行success,只能用浏览器开发工具看ajax返回结果

     <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery-1.11.3.js/"></script>
    
    点赞 评论
  • Be_Proud
    Be_Proud 2016-02-02 13:39




    UserNameServlet
    UserNameServlet
    com.servlets.UserNameServlet


    UserNameServlet
    /login

    这是xml

    点赞 评论
  • rui888
    兔子托尼啊 2016-02-03 00:57

    首先你的 方法进入的到你的后台 UserNameServlet 里面没有。

    点赞 评论

相关推荐