隨筆-86  評論-767  文章-3  trackbacks-3

          有網友反映說《AJAX開發簡略》配文代碼不全。其實應該是全的,只是要把包括框架和兩個示例的程序都整合起來看。這里把全部的代碼貼出來,需要的朋友可以看看。
          sample1_1.jsp:
          <%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
          <title>無標題文檔</title>
          <script language="javascript">
           var http_request = false;
           function send_request(url) {//初始化、指定處理函數、發送請求的函數
            http_request = false;
            //開始初始化XMLHttpRequest對象
            if(window.XMLHttpRequest) { //Mozilla 瀏覽器
             http_request = new XMLHttpRequest();
             if (http_request.overrideMimeType) {//設置MiME類別
              http_request.overrideMimeType('text/xml');
             }
            }
            else if (window.ActiveXObject) { // IE瀏覽器
             try {
              http_request = new ActiveXObject("Msxml2.XMLHTTP");
             } catch (e) {
              try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
              } catch (e) {}
             }
            }
            if (!http_request) { // 異常,創建對象實例失敗
             window.alert("不能創建XMLHttpRequest對象實例.");
             return false;
            }
            http_request.onreadystatechange = processRequest;
            // 確定發送請求的方式和URL以及是否同步執行下段代碼
            http_request.open("GET", url, true);
            http_request.send(null);
           }
           // 處理返回信息的函數
              function processRequest() {
                  if (http_request.readyState == 4) { // 判斷對象狀態
                      if (http_request.status == 200) { // 信息已經成功返回,開始處理信息
                          alert(http_request.responseText);
                      } else { //頁面不正常
                          alert("您所請求的頁面有異常。");
                      }
                  }
              }
           function userCheck() {
            var f = document.form1;
            var username = f.username.value;
            if(username=="") {
             window.alert("用戶名不能為空。");
             f.username.focus();
             return false;
            }
            else {
             send_request('sample1_2.jsp?username='+username);
            }
           }
          </script>
          <link href="css/style.css" rel="stylesheet" type="text/css">
          </head>

          <body>
          <form name="form1" action="" method="post">
          用戶名:<input type="text" name="username" value="">&nbsp;
          <input type="button" name="check" value="唯一性檢查" onClick="userCheck()">
          <input type="submit" name="submit" value="提交">
          </form>
          <!--span style="cursor: pointer; text-decoration: underline" onclick="send_request('2.jsp?username=educhina')">Send a request</span-->
          </body>
          </html>

          sample1_2.jsp:
          <%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
          <%
          String username= request.getParameter("username");
          if("educhina".equals(username)) out.print("用戶名已經被注冊,請更換一個用戶名。");
          else out.print("用戶名尚未被使用,您可以繼續。");
          %>

          sample2_1.jsp:
          <%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
          <title>無標題文檔</title>
          <script language="javascript">
           var http_request = false;
           var currentPos = null;
           function send_request(url) {//初始化、指定處理函數、發送請求的函數
            http_request = false;
            //開始初始化XMLHttpRequest對象
            if(window.XMLHttpRequest) { //Mozilla 瀏覽器
             http_request = new XMLHttpRequest();
             if (http_request.overrideMimeType) {//設置MiME類別
              http_request.overrideMimeType('text/xml');
             }
            }
            else if (window.ActiveXObject) { // IE瀏覽器
             try {
              http_request = new ActiveXObject("Msxml2.XMLHTTP");
             } catch (e) {
              try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
              } catch (e) {}
             }
            }
            if (!http_request) { // 異常,創建對象實例失敗
             window.alert("不能創建XMLHttpRequest對象實例.");
             return false;
            }
            http_request.onreadystatechange = processRequest;
            // 確定發送請求的方式和URL以及是否同步執行下段代碼
            http_request.open("GET", url, true);
            http_request.send(null);
           }
           // 處理返回信息的函數
              function processRequest() {
                  if (http_request.readyState == 4) { // 判斷對象狀態
                      if (http_request.status == 200) { // 信息已經成功返回,開始處理信息
                          //alert(http_request.responseText);
              document.getElementById(currentPos).innerHTML = http_request.responseText;
                      } else { //頁面不正常
                          alert("您所請求的頁面有異常。");
                      }
                  }
              }
           //顯示部門下的崗位
           function showRoles(obj) {
            document.getElementById(obj).parentNode.style.display = "";
            document.getElementById(obj).innerHTML = "正在讀取數據..."
            currentPos = obj;
            send_request("sample2_2.jsp?playPos="+obj);
           }
          </script>
          <link href="css/style.css" rel="stylesheet" type="text/css">
          </head>

          <body>
          <table width="200" border="0" cellspacing="0" cellpadding="0">
              <tr>
                  <td height="20"><a href="javascript:void(0)" onClick="showRoles('pos_1')">經理室</a></td>
              </tr>
              <tr style="display:none">
                  <td height="20" id="pos_1">&nbsp;</td>
              </tr>
              <tr>
                  <td height="20"><a href="javascript:void(0)" onClick="showRoles('pos_2')">開發部</a></td>
              </tr>
              <tr style="display:none ">
                  <td id="pos_2" height="20">&nbsp;</td>
              </tr>
          </table>
          <!--a href="javascript:void(0)" onClick="showRoles('pos_1')">測試</a-->
          <!--span style="cursor: pointer; text-decoration: underline" onclick="send_request('2.jsp?username=educhina')">Send a request</span-->
          </body>
          </html>

          sample2_2.jsp:
          <%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
          <%
          String playPos = request.getParameter("playPos");
          if("pos_1".equals(playPos)) out.print("&nbsp;&nbsp;總經理<br>&nbsp;&nbsp;副總經理");
          else if("pos_2".equals(playPos)) out.println("&nbsp;&nbsp;總工程師<br>&nbsp;&nbsp;軟件工程師");
          %>

          posted on 2005-11-01 09:26 eamoi 閱讀(7187) 評論(29)  編輯  收藏 所屬分類: AJAX

          評論:
          # re: 【原創】《AJAX開發簡略》配文代碼 2005-11-03 21:11 | emu
          document.getElementById(currentPos).innerHTML = http_request.responseText;

          不如叫AJAH算了。如果你知道我在說什么,我想你會同意我的說法的。  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2005-11-04 08:34 | eamoi
          了解您的意思。
          后續文章還在草稿中,內容主要關于DOM和XML的。
          謝謝您的關注。  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2005-11-04 13:06 | emu
          哈哈你要寫就趕快哈,我也在醞釀中。  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2005-11-04 13:21 | emu
          搜了一下,你的文章轉載的地方很多呀。看來現在這方面的中文資料真的是太缺乏了。
          可惜很多網站都沒有給出你的原文鏈接。以后寫技術文章也寫一個版權聲明上去好了。  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2005-11-04 15:13 | eamoi
          版權聲明已經寫了。原來的定位就是open doc。放到網絡上,版權問題只希望大家共同遵守。  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2005-11-06 15:42 | jiniboy
          有轉載不是壞事阿  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2005-11-09 13:28 | tilin
          今天剛開始學AJAX,很好.文章對我很有幫助,謝謝~~  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2005-11-18 11:11 | yoyozy
          看了您的文章收獲很大,有關ajax的中文資料真的很少,很期待這樣的文章。  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2005-11-19 23:13 | 謝孟軍
          很激動,現在還有像你這樣的無私的人.支持你的創作  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2005-11-21 11:35 | 白起
          今天看到一個帖子,提到ajax開發,通過百度搜索了知道你的這個文章,但我把你的代碼復制了,第一個怎么顯示的結果是把sample1_2.jsp的代碼顯示出來了  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2005-11-21 13:43 | eamoi
          肯定是你的服務器不支持JSP。正常sample1_2.jsp是要編碼后再返回的。  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2005-11-28 18:27 | jia
          有沒把http_request.open("POST", url, true);
          的寫法!!  回復  更多評論
            
          # sample1_2.jsp里面有個錯誤 2005-11-29 15:48 | 金海龍
          <%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
          <%
          String playPos = request.getParameter("username");
          if("someone".equals(playPos)) out.print("用戶名已經被注冊,請更換一個用戶名。");
          else out.print("用戶名尚未被使用,您可以繼續。");
          %>
            回復  更多評論
            
          # 我想你原來的應該是這個 2005-11-29 15:50 | 金海龍
          <%@ page contentType="text/html; charset=gb2312" errorPage="" %>

          <%

          String username = request.getParameter("username");

          if("educhina".equals(username)) out.print("用戶名已經被注冊,請更換一個用戶名。");

          else out.print("用戶名尚未被使用,您可以繼續。");

          %>


            回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2005-11-30 12:55 | eamoi
          謝謝你指出來。代碼貼錯了,抱歉。  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2006-01-13 11:54 | sonoky
          ASP怎么用?我把sample1_2.jsp,sample1_2.jsp改為asp文件后就一直是請求的頁面異常.難道只能用jsp嗎?

          <%
          dim username
          username=request("username")
          if username="abc"
          response.write("用戶名已經存在!")
          else
          response.write("可以注冊!")
          end if
          %>  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2006-01-13 14:49 | sonoky

          上次的代碼少了一個then
          可現在是
          <%
          dim username
          username=request("username")
          if username="abc" then
          response.write("用戶名已經存在!")
          else
          response.write("可以注冊!")
          end if
          %>
          卻也不行,提示系統錯誤!  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2006-01-13 14:56 | sonoky
          明白了,要定義字符集,否則只能彈出非漢字

          <%
          Response.Charset="gb2312"
          if request("username")="abc" then
          response.write("用戶名存在")
          else
          response.write("可以注冊")
          end if
          %>
          這樣就OK了!!!  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2006-01-23 14:31 | hinz
          看完你的文章對AJAX有了較完整的認識,謝謝  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2006-03-31 13:40 | eDust
          照這樣看ajax 可以和任意一種服務器端程序搭配使用!  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2006-04-07 08:49 | 網頁上有錯誤
          不斷提示網頁上有錯誤  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2006-04-17 10:26 | keepinglive
          非常感謝,我想通過你的例子我已經算是入了門。哈哈,繼續努力!  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2006-06-05 19:57 | cntime
          感謝您的分享  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2006-06-05 19:57 | cntime
          你這個AJAX提交是怎么做的啊?  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2006-09-18 09:36 | hongloum
          謝謝  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2006-10-02 10:40 | wf
          sample8_1.htm
          讀取xml怎么總提示頁面異常啊
          wf768012@hotmail.com  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2006-11-12 18:19 | gdg
          gdfgd
          gdgfd  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2007-03-09 17:40 | cqq
          http_request.status == 0是為什么???  回復  更多評論
            
          # re: 【原創】《AJAX開發簡略》配文代碼 2008-04-25 17:25 | question
          @sonoky
          @cqq
          有沒有評論以及對評論回復的例子,加上分頁
            回復  更多評論
            
          主站蜘蛛池模板: 武胜县| 班玛县| 岢岚县| 电白县| 襄城县| 瑞金市| 华安县| 朝阳区| 兴宁市| 景宁| 河南省| 油尖旺区| 济宁市| 石嘴山市| 江陵县| 八宿县| 宜兰县| 大同市| 河间市| 神池县| 尚志市| 富裕县| 定兴县| 灯塔市| 博野县| 藁城市| 囊谦县| 新津县| 萍乡市| 大宁县| 炉霍县| 铜鼓县| 门源| 南川市| 高阳县| 汶川县| 航空| 临武县| 香格里拉县| 南漳县| 福贡县|