[AJAX] 간단한 AJAX 호출 함수

By | 6월 4, 2009

//xmlHttp 객체 생성
function createHttp() {
 try {
  return new XMLHttpRequest();
 }
 catch (e) {
  var objectNames = ["MSXML2.XMLHTTP.5.0",
    "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0",
    "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
  for (var i = 0; i < objectNames.length; i ++) {
   try {
    return new ActiveXObject(objectNames[i]);
    break;
   }
   catch (e) {
   }
  }
  return null;
 }
}

// AJAX 요청을 보내는 함수, 결과값을 처리할 함수를 사용자 정의해야 함.
//
// url : 호출하고자 하는 URL
// parameters : 전달하고자 하는 파라미터  (ex) country=korea&city=seoul
// responseType : TEXT로 응답을 받고자 할 때 - "text" / XML로 응답을 받고자 할 때 - "xml"
// callbackFunc : ajax response를 인자로 하여 결과를 뿌려줄 함수의 이름
//
// (사용예)
//          requestAjax("/index.do","name=bruce&sex=male", "text", onResponse);
//
//          function onResponse(response){
//            alert(response);
//          }

function requestAjax(url, parameters, responseType, callbackFunc){

    var encodedParam = encodeURI(parameters);       //POST 방식일 경우에는 인코딩을 해 주어야 한다.
    var xmlhttp = createHttp();
    xmlhttp.open("post", url, true);

    var varResponseStatusCode = "";
    var varResponseHeaders = "";
    var varResponseContents = "";

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState==4) {
            varResponseStatusCode = xmlhttp.status;
            varResponseHeaders = xmlhttp.getAllResponseHeaders();
            if (xmlhttp.status==200) {

                if(responseType == "xml"){
                    varResponseContents = xmlhttp.responseXML;
                }else if(responseType == "text"){
                    varResponseContents = xmlhttp.responseText;
                }
                callbackFunc(varResponseContents);
            }
        }
    }

//POST방식으로 전송할 때에는 setRequestHeader를 반드시 이렇게 설정해 주어야 한다고 한다.
//이 부분을 잘못 적으면 WAS에 따라서는 민감하게 반응하여 에러를 내는 듯 하다.
//이전에 Ajax 호출시 WebLogic에서 에러를 뿜어내던 적이 있었는데
//그 때 charset=UTF-8옆에 ; 이 붙어 있어서 인식을 못했던 것이었다.
//여기에서 에러가 날 경우 대소문자나 공백도 다시 한번 점검해 보자.
//WebLogic 에서의 에러메세지는 아래에 붙여넣었다.

    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    xmlhttp.send(parameters);

    this.responseStatusCode = varResponseStatusCode;
    this.responseHeaders = varResponseHeaders;
    this.responseContents = varResponseContents;
}

 

// AJAX 요청을 보낸 후 응답 TEXT를, ID값이 elementId인 HTML엘리먼트의 innerHTML로 삽입하는 함수.

// elementId : 결과값을 삽입할 HTML엘리먼트의 id
// url : 호출하고자 하는 URL
// parameters : 전달하고자 하는 파라미터  (ex) country=korea&city=seoul
//
// (사용예 : 'div01' 이라는 id를 가지는 HTML엘리먼트가 문서 내에 존재할 때)
//
//          updateAjax("div01", "/index.do", "name=bruce&sex=male");
//

function updateAjax(elementId, url, parameters){

    var obj = document.getElementById(elementId);
    var xmlhttp = createHttp();
    xmlhttp.open("post", url, true);

    var varResponseStatusCode = "";
    var varResponseContents = "";

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState==4) {
            varResponseStatusCode = xmlhttp.status;
            if (xmlhttp.status==200) {
                varResponseContents = xmlhttp.responseText;
                obj.innerHTML = varResponseContents;
            }
        }
    }

    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    xmlhttp.send(encodeURI(parameters));

    this.responseStatusCode = varResponseStatusCode;
    this.responseContents = varResponseContents;
}

* 출처 : 인터넷의 어느 분의 글을 참고하여 작성하였으나 잘 기억이 안남 --;

* setRequestHeader 의 내용이 틀렸을 때 WebLogic 에서 뿜어내던 메세지

<2009. 6. 25 ?ㅼ쟾 11??18?18?KST> <Error> <HTTP> <BEA-101108> <[weblogic.servlet.internal.WebAppServletContext@4568f8 - appName: 'front', name: 'front3', context-path: ''] Unsupported encoding: "UTF-8;" specified.

java.io.UnsupportedEncodingException: Unsupported Encoding UTF-8;

        at weblogic.servlet.internal.ServletRequestImpl.setCharacterEncoding(ServletRequestImpl.java:384)

        at weblogic.servlet.internal.ServletRequestImpl.initReader(ServletRequestImpl.java:1064)

        at weblogic.servlet.internal.ServletRequestImpl.access$1100(ServletRequestImpl.java:71)

        at weblogic.servlet.internal.ServletRequestImpl$RequestParameters.mergePostParams(ServletRequestImpl.java:1771)

        at weblogic.servlet.internal.ServletRequestImpl$RequestParameters.parseQueryParams(ServletRequestImpl.java:1696)

        at weblogic.servlet.internal.ServletRequestImpl$RequestParameters.getQueryParams(ServletRequestImpl.java:1649)

        at weblogic.servlet.internal.ServletRequestImpl.getParameter(ServletRequestImpl.java:747)

        at com.samsungmobile.forum.front.mbr.web.filter.MbrSessionFilter.doFilter(MbrSessionFilter.java:104)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)

        at com.samsungmobile.forum.common.cmm.web.filter.SSLFilter.doFilter(SSLFilter.java:109)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)

        at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:39)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)

        at kr.co.samsungnetworks.snifw.filter.CharacterEncodingFilter.doFilter(Unknown Source)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)

        at netville.servlet.filter.AuditFilter.doFilter(AuditFilter.java:78)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)

        at net.uzen.web.common.filter.CommonFilter.doFilter(CommonFilter.java:30)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)

        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3243)

        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)

        at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)

        at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2003)

        at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:1909)

        at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1359)

        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)

        at weblogic.work.ExecuteThread.run(ExecuteThread.java:181)

>

<2009. 6. 25 ?ㅼ쟾 11??18?18?KST> <Error> <HTTP> <BEA-101019> <[weblogic.servlet.internal.WebAppServletContext@4568f8 - appName: 'front', name: 'front3', context-path: ''] Servlet failed with IOException

weblogic.utils.NestedRuntimeException: Cannot parse POST parameters of request: '/bbs/innovlab/sendVote.do'

        at weblogic.servlet.internal.ServletRequestImpl$RequestParameters.mergePostParams(ServletRequestImpl.java:1809)

        at weblogic.servlet.internal.ServletRequestImpl$RequestParameters.parseQueryParams(ServletRequestImpl.java:1696)

        at weblogic.servlet.internal.ServletRequestImpl$RequestParameters.getQueryParams(ServletRequestImpl.java:1649)

        at weblogic.servlet.internal.ServletRequestImpl.getParameter(ServletRequestImpl.java:747)

        at com.samsungmobile.forum.front.mbr.web.filter.MbrSessionFilter.doFilter(MbrSessionFilter.java:104)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)

        at com.samsungmobile.forum.common.cmm.web.filter.SSLFilter.doFilter(SSLFilter.java:109)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)

        at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:39)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)

        at kr.co.samsungnetworks.snifw.filter.CharacterEncodingFilter.doFilter(Unknown Source)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)

        at netville.servlet.filter.AuditFilter.doFilter(AuditFilter.java:78)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)

        at net.uzen.web.common.filter.CommonFilter.doFilter(CommonFilter.java:30)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)

        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3243)

        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)

        at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)

        at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2003)

        at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:1909)

        at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1359)

        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)

        at weblogic.work.ExecuteThread.run(ExecuteThread.java:181)

java.io.UnsupportedEncodingException: Unsupported Encoding UTF-8;

        at weblogic.servlet.internal.ServletRequestImpl.setCharacterEncoding(ServletRequestImpl.java:384)

        at weblogic.servlet.internal.ServletRequestImpl.initReader(ServletRequestImpl.java:1064)

        at weblogic.servlet.internal.ServletRequestImpl.access$1100(ServletRequestImpl.java:71)

        at weblogic.servlet.internal.ServletRequestImpl$RequestParameters.mergePostParams(ServletRequestImpl.java:1771)

        at weblogic.servlet.internal.ServletRequestImpl$RequestParameters.parseQueryParams(ServletRequestImpl.java:1696)

        at weblogic.servlet.internal.ServletRequestImpl$RequestParameters.getQueryParams(ServletRequestImpl.java:1649)

        at weblogic.servlet.internal.ServletRequestImpl.getParameter(ServletRequestImpl.java:747)

        at com.samsungmobile.forum.front.mbr.web.filter.MbrSessionFilter.doFilter(MbrSessionFilter.java:104)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)

        at com.samsungmobile.forum.common.cmm.web.filter.SSLFilter.doFilter(SSLFilter.java:109)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)

        at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:39)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)

        at kr.co.samsungnetworks.snifw.filter.CharacterEncodingFilter.doFilter(Unknown Source)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)

        at netville.servlet.filter.AuditFilter.doFilter(AuditFilter.java:78)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)

        at net.uzen.web.common.filter.CommonFilter.doFilter(CommonFilter.java:30)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)

        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3243)

        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)

        at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)

        at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2003)

        at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:1909)

        at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1359)

        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)

        at weblogic.work.ExecuteThread.run(ExecuteThread.java:181

 

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments