ajax非同步伺服器擷取時間

來源:互聯網
上載者:User

標籤:

1、建立ajax對象

    <script type="text/javascript">        //建立AJAX非同步對象        function createAJAX(){            var ajax = null;            try{                //如果IE5=IE12的話                ajax = new ActiveXObject("microsoft.xmlhttp");            }catch(e1){                try{                    //如果是非IE的話                    ajax = new XMLHttpRequest();                }catch(e2){                    alert("你的瀏覽器中不支援非同步對象,請換瀏覽器");                }            }            return ajax;        }    </script>

2、添加擷取時間按鈕

目前時間:<span id="time"></span><br/><input id="buttonID" type="button" value="擷取目前時間"/><p/>

3、HTML內對ajax返回的資料進行處理

<script type="text/javascript">        document.getElementById("buttonID").onclick = function(){            //NO1)建立AJAX非同步對象            var ajax = createAJAX();            //NO2)準備發送請求            var method = "GET";            var url = "${pageContext.request.contextPath}/AjaxTimeServlet?time=" + new Date().getTime();            ajax.open(method,url);            //NO3)真正發送請求體的資料到伺服器,如果請求體中無資料的話,就用null表示            ajax.send(null);                        //-------------------------------------------------------------等待                    //NO4)AJAX非同步對象不斷監聽伺服器響應的狀態0-1-2-3-【4】            //一定要狀態變化後,才可觸發function(){}函數            //如果狀態永遠是4-4-4-4-4,是不會觸發function(){}函數的            ajax.onreadystatechange = function(){                //如果狀態代碼為4的話                if(ajax.readyState == 4){                    //如果響應碼為200的話                    if(ajax.status == 200){                        //NO5)從AJAX非同步對象中擷取伺服器響應的HTML資料                        var nowStr = ajax.responseText;                                                //NO6)將結果按照DOM規則,動態添加到web頁面指定的標籤中                        var spanElement = document.getElementById("time");                        spanElement.innerHTML = nowStr;                    }                }            }                     }

4、java對ajax的處理

/** * 無需重新整理整個Web頁面顯示伺服器響應的目前時間 */public class AjaxTimeServlet extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        String nowStr = sdf.format(new Date());                //以流的方式將結果響應到AJAX非同步對象中        response.setContentType("text/html;charset=UTF-8");        PrintWriter pw = response.getWriter();        pw.write(nowStr);        pw.flush();        pw.close();    }}

 

ajax非同步伺服器擷取時間

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.