JavaScript

자바스크립트 라이브러리
  • 조회수 2,225
  • 작성일 2008-07-30
  •  

function createREQ()
{
 try
 {
  req = new XMLHttpRequest();
 }
 catch (err1)
 {
  try
  {
   req = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (err2)
  {
   try
   {
    req = new ActiveXObject("Microsoft.XMLHTTP");
   }
   catch (err3)
   {
    req = false;
   }
  }
 }
 return req;
}

function requestGET(url, query, req)
{
 myRand = new Date().getTime();
 //myRand = parseInt(Math.random()*999999999);
 req.open("GET", url+'?'+query+'&rand='+myRand, true);
 req.send(null);
}

function requestPOST(url, query, req)
{
 req.open("POST", url, true);
 req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
 req.send(query);
}

function doCallback(callback, item)
{
 eval(callback + '(item)');
}

function doAjax(url, query, callback, reqtype, getxml)
{
 var myreq = createREQ();

 myreq.onreadystatechange = function()
 {
  if (myreq.readyState == 4)
  {
   if (myreq.status == 200)
   {
    var item = myreq.responseText;
    if (getxml == 1)
    {
     item = myreq.responseXML;
    }
    doCallback(callback, item);
   }
  }
 }
 if (reqtype == 'post')
 {
  requestPOST(url, query, myreq);
 } else {
  requestGET(url, query, myreq);
 }
}

[이 게시물은 Adma님에 의해 2008-07-30 10:04:50 AJAX에서 복사 됨]