쓸만한 주저리

javascript로 ajax 쓰기

봄돌73 2006. 4. 7. 11:33

function process_action( loaded_data, layer_name )
{
 layer= document.getElementById(layer_name);
// layer.style.display ='block';
 layer.innerHTML = loaded_data;
 //추가 처리용 함수가 있으면 실행
 if (load_action)
 {
  eval(load_action)
 }
}

 

function load_url(url, layer_name){
 /**
  * request object
  */
 var req;
 /**
  * target layer
  */
 var layer;

 /**
  * XMLHttpRequest request handler
  */
 function request_handler()
 {
  // redayState 4이면 complete
  if (req.readyState == 4) {
   // 200 이면 ok
   if (req.status == 200) {
    // 특정 실행
    process_action( req.responseText, layer_name);
   } else {
 //            alert("There was a problem retrieving  data:\n" + req.statusText);
   }
  }
 }
 //make request
 if (window.XMLHttpRequest)
 {
  req = new XMLHttpRequest();
  req.onreadystatechange = request_handler;
  req.open("get", url, true);
  req.send(null);
 }
 else if (window.ActiveXObject)
 {
  req = new ActiveXObject("Microsoft.XMLHTTP");
  if (req) {
   req.open("get", url, true);
      req.setRequestHeader("Accept-Language","ko");
      req.setRequestHeader("Content-type:", "text/html; charset='UTF-8'");
//    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   req.send();
   req.onreadystatechange = request_handler;
  }
 }
}

 

이 방식으로 안되는 경우도 가끔 보인다.