IT_Programming/AJAX · Atlas

XMLHttpRequest + InnerHTML을 적용한 간단한 예제..

JJun ™ 2009. 2. 6. 00:37

 

별것 아닌 내용이지만.. 복습 삼아... 끄적여봄~

==================================================================================================

 

 

 

==================================================================================================

 

[httpRequest.js]

 

function getXMLHttpRequest()
{
       if (window.ActiveXObject) // 마이크로 소프트 IE
       {
              try 
              {
                     return new ActiveXObject("Msxml2.XMLHTTP"); // IE 업 버전
              } 
              catch(e) 
              {
                     try 
                     {
                            return new ActiveXObject("Microsoft.XMLHTTP"); // IE 다운 버전
                     } 
                     catch(e1) 
                     { 
                            return null; 
                     }
              }
       }       
       else if (window.XMLHttpRequest) // 기타 웹 브라우저들
       {
              return new XMLHttpRequest();
       } 
       else 
       {
              return null;
       }

}

var httpRequest = null;

 

function sendRequest(url, params, callback, method)
{
       httpRequest = getXMLHttpRequest();
 
       var httpMethod = method ? method : 'GET';
       if (httpMethod != 'GET' && httpMethod != 'POST') 
       {
              httpMethod = 'GET';
       }
 
       var httpUrl = url;
       var httpParams = (params == null || params == '') ? null : params;
       if (httpMethod == 'GET' && httpParams != null) 
       {
              httpUrl = httpUrl + "?" + httpParams;
       }
 
       // open(): 요청의 초기화, GET/POST 선택, 접속할 URL 입력
       httpRequest.open(httpMethod, httpUrl, true);
       httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
       httpRequest.onreadystatechange = callback; // 콜백함수 등록
 
       // send(): 웹 서버에 요청 전송
       httpRequest.send(httpMethod == 'POST' ? httpParams : null);
}

 

--------------------------------------------------------------------------------------------------


[news.html]

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

      "http://www.w3.org/TR/html4/loose.dtd">

<html>
      <head>
            <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
            <title>뉴 스</title>
            <script type="text/javascript" src="../JS/httpRequest.js"></script>
            <script type="text/javascript">
                   function loadNews()
                   {
                          sendRequest("getNewsTitle.jsp", null, loadedNews, "GET");
                   }
 
                   function loadedNews()
                   {
                          if(httpRequest.readyState == 4)
                          {
                                 if(httpRequest.status == 200)
                                 {
                                        var newsList = document.getElementById("newsList");
                                        newsList.innerHTML = httpRequest.responseText;
                                 }
                          }
                   }
                  /* 
                   window.onload = function() // 화면 로드시..
                   {
                          loadNews();
                   }
                  */ 

            </script>
      </head>
      <body>
             <input type="button" value ="기사보기" onclick="loadNews()">
             <br><br>
             <div id="newsList"></div>
      </body>
</html>

 

--------------------------------------------------------------------------------------------------

 

[getNewsTitle.jsp]

 

<%@ page language="java" contentType="text/plain; charset=euc-kr" %>

<%
       String[] titles = 
       {
              "NBA 한 경기 최다 득점은? ",
              "차세대 황제 르브론 제임스..",
              "코비 브라이언트 메디슨 스퀘어 가든에서 61점 역대 최고 득점"
       };

 

       for(int i=0; i<titles.length; i++)
       {
%>

      <% if(i==0) { %> <strong> <% } %>
             <%= titles[i] %>
      <% if(i==0) { %></strong><% } %>
       <br />
<% } %>

 

--------------------------------------------------------------------------------------------------