码迷,mamicode.com
首页 > Web开发 > 详细

Ajax

时间:2017-10-19 10:35:28      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:web   div   .ajax   get   cti   return   lang   exception   state   

技术分享
@WebServlet(urlPatterns = {"/FristServlet"},name = "first")
public class FristServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uname=request.getParameter("uname");
        if(uname.equals("admin")){
            response.getWriter().write("true");
        }else{
            response.getWriter().write("false");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
servlet层
技术分享
public class Book {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
entity层
技术分享
<%@page language="java" pageEncoding="utf-8" isELIgnored="false" %>
<html>
<script type="text/javascript" src="js/jquery-1.8.3.js"></script>
<script type="text/javascript">
   $(function () {
      $("[name=uname]").blur(function(){
        /*oldAjax();*/
       /* newAjax();*/
       //get
        /*  $.get(‘/FristServlet‘,{"uname":$("[name=uname]").val()},function (data) {
              $("#msg").html(data);
          })*/
          //post
         /* $.post(‘/FristServlet‘,{"uname":$("[name=uname]").val()},function (data) {
              $("#msg").html(data);
          })*/
         //getJSON
          $.getJSON(‘/FristServlet‘,{"uname":$("[name=uname]").val()},function (data) {
             alert(typeof data);
              $("#msg").html(data);
          })
       });
       var userArray = [ {
           "id" : 2,
           "name" : "admin",
           "pwd" : "123"
       }, {
           "id" : 3,
           "name" : "詹姆斯",
           "pwd" : "11111"
       }, {
           "id" : 4,
           "name" : "梅西",
           "pwd" : "6666"
       } ];
//在table内部追加一行  tr
       var $table = $("<table border=‘1‘></table>").append("<tr><td>ID</td><td>用户名</td><td>密码</td></tr>");
       $.each(userArray,function(i,dom) {
           $table.append("<tr><td>" + dom.id+ "</td><td>" + dom.name+ "</td><td>"+ dom.pwd + "</td></tr>");
       });
       $("#objectArrayDiv").append($table);
       
   });


function newAjax() {
    $.ajax({
        url:"/FristServlet",
        type:"POST",
        data:{"uname":$("[name=uname]").val()},
        async:true,
        success:function (data) {
            $("#msg").html(data);
        }
    });
}
//基础版
    function oldAjax() {
        var xhr=null;
        var as=$("[name=uname]").val();
       if(window.XMLHttpRequest){
          xhr=new XMLHttpRequest();
       } else{
           xhr=new ActiveXObject();
       }
       xhr.open("post","/FristServlet",true);
       xhr.setRequestHeader("Content-Type","application/x-www-from-urlencoded");
       xhr.send("uname="+as);
       xhr.onreadystatechange=function () {
           if(xhr.readyState==4&&xhr.status==200){
               var data=xhr.responseText;
               $("#msg").html(data);
           }
       }
    }
</script>
<body>
<h2>Hello World!</h2>
<form id="from1">
    <input name="uname"/><span id="msg"></span>
    <input name="upwd"/>
</form>
<div id="objectArrayDiv"></div>
</body>
</html>
jsp页面

 

Ajax

标签:web   div   .ajax   get   cti   return   lang   exception   state   

原文地址:http://www.cnblogs.com/with-lj/p/7678716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!