标签: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); } }
public class Book { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
<%@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>
标签:web div .ajax get cti return lang exception state
原文地址:http://www.cnblogs.com/with-lj/p/7678716.html