开发者社区> 问答> 正文

关于 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>

展开
收起
51干警网 2016-02-17 21:55:35 2680 0
1 条回答
写回答
取消 提交回答
  • 阿里云论坛版主,QQ 1978638808
    采纳回答

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

     <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery-1.11.3.js/"></script>
    2019-07-17 18:29:14
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
23-Vue.js在前端...1506518547.pdf 立即下载
Javascript异步编程 立即下载
JavaScript异步编程 立即下载

相关实验场景

更多