Thking In Java

          關注應用程序的HA、可擴展性 多實踐,遇到問題查doc,google,上論壇咨詢

           

          [導入]用JQuery實現(xiàn)ajax提交時頁面顯示正在提交的提示

          一:功能要求
          頁面進行ajax提交的時候,在服務端未響應的時間內(nèi),頁面出現(xiàn)“正在加載”的文字提示,告訴用戶他的請求已得到響應,得到服務端響應后,提示關閉不顯示。
          二:代碼
          1.首先是web頁面:
           當點擊ID未span_1的文字后。發(fā)送一個get請求。
           <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
          <title>Loading demo</title>
          <script src="jquery-1.2.1.min.js" type="text/javascript"></script>
          <script type="text/javascript">
              $(document).ready(function() {
                  $("#span_1").click(function() {
                      //alert("click");
                      $.get("Faq",{id:1}, function(xml){
                          var text = $("Response", xml).text();
                          $("#div_1").html(text);
                      });
                  });
                  $("#loading").ajaxStart(function(){
                      //alert("start");
                     //$(this).html("<img src=loading.gif>&nbsp;正在加載");   
                      $(this).css("display", "block");
                  });
                  $("#loading").ajaxSuccess(function(){
                      //alert("end");
                      //$(this).html("加載完成!");
                      $(this).css("display", "none");
                  });
              });
          </script>
          </head>
          <body>

          <div id="faq">
          <span id="span_1">什么是jquery?</span>
          <div id="div_1"></div>
          </div>
          <div id="loading" style="display:none"><img src=loading.gif>&nbsp;正在加載</div>

          </body>
          </html>


          2.服務端
           服務端是一個servlet,隨即顯示一段文字。
           package com.hexin.ajax;

          import java.io.IOException;
          import java.io.PrintWriter;
          import java.util.Random;

          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          public class Faq extends HttpServlet {

              private final String XML_HEAD = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
              /**
               * Constructor of the object.
               */
              public Faq() {
                  super();
              }

              /**
               * Destruction of the servlet. <br>
               */
              public void destroy() {
                  super.destroy(); // Just puts "destroy" string in log
                  // Put your code here
              }

              /**
               * The doGet method of the servlet. <br>
               *
               * This method is called when a form has its tag value method equals to get.
               *
               * @param request the request send by the client to the server
               * @param response the response send by the server to the client
               * @throws ServletException if an error occurred
               * @throws IOException if an error occurred
               */
              public void doGet(HttpServletRequest request, HttpServletResponse response)
                      throws ServletException, IOException {
              
                  try {
                      Thread.sleep(1000);
                  } catch (InterruptedException e) {
                      
                      e.printStackTrace();
                  }
                  System.out.println("開始執(zhí)行");
                  response.setContentType("text/xml");
                  response.setHeader("Cache-Control", "no-cache");
                  response.setCharacterEncoding("UTF-8");//設置utf-8j解決亂碼問題
                  
                  
                  
                  PrintWriter out = response.getWriter();
                  out.print(XML_HEAD);
                  
                  Random random = new Random(System.currentTimeMillis());
                  if(random.nextBoolean()){
                      out.println("<Response>論壇版主用戶可以根據(jù)自己的特長喜好自愿申請,申請版主的用戶應該是累積經(jīng)驗值達到1000分,威望值達到20以上的注冊用戶。版主需要承擔相應的版面發(fā)展職責,并愿意為其他用戶服務。</Response>");
                      System.out.println("至個人信息管理中心可以進行基本信息管理、社區(qū)管理與論壇管理,可以查看短消息、整理個人收藏、查看個人主題,直接點擊頁面右側(cè)相關鏈接即可。");
                  }else{
                      out.println("<Response>論壇版主用戶可以根據(jù)自己的特長喜好自愿申請,申請版主的用戶應該是累積經(jīng)驗值達到1000分,威望值達到20以上的注冊用戶。版主需要承擔相應的版面發(fā)展職責,并愿意為其他用戶服務。</Response>");
                      System.out.println("論壇版主用戶可以根據(jù)自己的特長喜好自愿申請,申請版主的用戶應該是累積經(jīng)驗值達到1000分,威望值達到20以上的注冊用戶。版主需要承擔相應的版面發(fā)展職責,并愿意為其他用戶服務。");
                  }
                  out.flush();
                  out.close();
              }

              /**
               * The doPost method of the servlet. <br>
               *
               * This method is called when a form has its tag value method equals to post.
               *
               * @param request the request send by the client to the server
               * @param response the response send by the server to the client
               * @throws ServletException if an error occurred
               * @throws IOException if an error occurred
               */
              public void doPost(HttpServletRequest request, HttpServletResponse response)
                      throws ServletException, IOException {

                  doGet(request, response);
              }

              /**
               * Initialization of the servlet. <br>
               *
               * @throws ServletException if an error occure
               */
              public void init() throws ServletException {
                  // Put your code here
              }

          }




          文章來源:http://huxiaofei590.blog.163.com/blog/static/32596122007111441412585

          posted on 2007-12-14 16:14 ThinkInJava 閱讀(4422) 評論(4)  編輯  收藏

          評論

          # re: [導入]用JQuery實現(xiàn)ajax提交時頁面顯示正在提交的提示 2014-06-26 10:44 ss

          ssss  回復  更多評論   

          # re: [導入]用JQuery實現(xiàn)ajax提交時頁面顯示正在提交的提示 2014-06-26 10:44 ss

          cccccccccccccccccc  回復  更多評論   

          # re: [導入]用JQuery實現(xiàn)ajax提交時頁面顯示正在提交的提示 2015-05-04 10:09 qw

          aad  回復  更多評論   

          # re: [導入]用JQuery實現(xiàn)ajax提交時頁面顯示正在提交的提示 2016-08-02 20:11

          ddf  回復  更多評論   


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           

          導航

          統(tǒng)計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章檔案

          java

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 邹平县| 民和| 昂仁县| 乌什县| 阿勒泰市| 许昌县| 新兴县| 五寨县| 武定县| 浪卡子县| 阿坝| 甘孜| 抚宁县| 昌图县| 双城市| 滦平县| 星座| 五家渠市| 英吉沙县| 罗江县| 安宁市| 恩平市| 金门县| 遵化市| 临桂县| 资溪县| 澎湖县| 涟源市| 肇源县| 攀枝花市| 金坛市| 湘阴县| 镇宁| 东辽县| 贵定县| 阳原县| 灯塔市| 克东县| 高邮市| 涟水县| 泰州市|