Ajax与JSON技术

前端之家收集整理的这篇文章主要介绍了Ajax与JSON技术前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Ajax实现简单的验证:

  1. <html>
  2. <head>
  3. <Metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
  4. <title>Inserttitlehere</title>
  5. <scripttype="text/javascript">
  6. functionajaxValid(){
  7. varxhr=null;
  8. varusername=document.getElementById("username").value;
  9. //创建XMLHttpRequst对象,今后主要靠此对象进行与后台交互
  10. if(window.ActiveXObject){
  11. //IE5,6创建对象的方式
  12. xhr=newActiveXObject("Microsoft.XMLHTTP");
  13. }else{
  14. xhr=newXMLHttpRequest();
  15. }
  16. //打开连接
  17. xhr.open("get",'/Web030Ajax/AjaxServlet?username='+username);
  18. //发送请求
  19. xhr.send(null);
  20. xhr.onreadystatechange=function(){
  21. //readyState码,0代表未初始化,1正在加载2已加载3正在交互4完成
  22. if(xhr.readyState==4){
  23. //服务器响应码,200成功
  24. if(xhr.status==200){
  25. // console.log('成功');
  26. varjsondata=JSON.parse(xhr.responseText);
  27. //alert(jsondata.info);
  28. document.getElementById("info").innerHTML=jsondata.info;
  29. }
  30. }
  31. };
  32. }
  33.  
  34. </script>
  35. </head>
  36. <body>
  37. <formaction="">
  38. <table>
  39. <tr>
  40. <td>用户名</td>
  41. <td><inputtype="text"name="username"id="username"onblur="ajaxValid()"/><spanid="info"></span></td>
  42. </tr>
  43. <tr>
  44. <td>密码</td>
  45. <td><inputtype="password"name="password"id="password"/></td>
  46. </tr>
  47.  
  48. </table>
  49.  
  50. </form>
  51. </body>
  52. </html>

servlet端

  1. protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
  2. //TODOAuto-generatedmethodstub
  3. Stringusername=request.getParameter("username");
  4. PrintWriterout=response.getWriter();
  5. if(username.equals("admin")){
  6. out.print("{\"info\":\"exit\"}");
  7. }else{
  8. out.print("{'info':'ok'}");
  9. }
  10. }

省份

  1. <head>
  2. <Metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
  3. <title>Inserttitlehere</title>
  4.  
  5. <scripttype="text/javascript">
  6.  
  7. functionff(){
  8. varselected=document.getElementById("selected1");
  9. selected.onclick=function(){
  10. varxhr=null;
  11. xhr=newXMLHttpRequest();
  12. xhr.open("post","/Web030Ajax/Province");
  13. xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//注意这句话的顺序
  14. xhr.send(null);
  15. xhr.onreadystatechange=function(){
  16. if(xhr.readyState==4){
  17. varprostr=xhr.responseText;
  18. vararry=JSON.parse(prostr);
  19. document.getElementById("selected").innerHTML='';
  20. for(vari=0;i<arry.length;i++){
  21. document.getElementById("selected").innerHTML+='<option>'+arry[i]+'</option>';
  22. }
  23. }
  24. };
  25. }
  26. }
  27.  
  28. </script>
  29. </head>
  30. <bodyonload="ff()">
  31. <formaction="">
  32. <selectid="selected">
  33. </select>
  34. <inputtype="button"value="dianwo"id="selected1">
  35. </form>
  36.  
  37. </body>
  38. </html>

servlet端

  1. protectedvoiddoPost(HttpServletRequestrequest,IOException{
  2. //TODOAuto-generatedmethodstub
  3. response.setCharacterEncoding("UTF-8");
  4. PrintWriterout=response.getWriter();
  5. List<String>provinces=newArrayList<String>();
  6. provinces.add("山东");
  7. provinces.add("北京");
  8. provinces.add("上海");
  9. Stringjsondata=JSONArray.fromObject(provinces).toString();
  10. out.print(jsondata);
  11. out.close();
  12. }

猜你在找的Ajax相关文章