통신 Ajax (XMLHttpRequest) 샘플

황제낙엽 2017.08.01 14:40 조회 수 : 93

sitelink1 https://www.omnibuscode.com/board/index...._srl=56147 
sitelink2  
sitelink3  
sitelink4  
extra_vars4  
extra_vars5  
extra_vars6  

/**

 * path : 요청받을 서비스페이지

 * params : 전송할 데이터 (name1=value1&name2=value2&name3=value3)

 */

function request(path, params) {
    var _ajax = new XMLHttpRequest();
    _ajax.onreadystatechange = function() {
        if (checkAjaxSuc(_ajax)) {
            var resMsg = _ajax.responseText;
            if (resMsg.length > 0) alert(resMsg);
        }
    };

 

    // POST 방식으로 요청시
    _ajax.open("POST", path);
    _ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

    try {
        _ajax.send(params);
    } catch (e) {        
        if (e.message && e.message.indexOf("0x80004005") > -1) {
            return;
        }
    }

 

    // GET 방식으로 요청시

    /***

    _ajax.open("GET", path+"?"+params);

    _ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

    try {
        _ajax.send("");
    } catch (e) {        
        if (e.message && e.message.indexOf("0x80004005") > -1) {
            return;
        }
    }

    ***/

}

 

 

function checkAjaxSuc(ajax) {
    if (ajax.readyState == 4) {
        try {
            return ( ajax.status  >= 200 &&  ajax.status < 300);
        } catch (ignore) {
            return false;
        }
    }
    return false;
}

번호 제목 글쓴이 날짜 조회 수
197 XMLHttpRequest.timeout 황제낙엽 2018.11.03 248
196 IFrames and cross-domain security file 황제낙엽 2012.01.13 246
195 UTF-8 한글 초성 추출 (자바스크립트) 황제낙엽 2019.05.07 243
194 Why does this simple Javascript prototype not work in IE? 황제낙엽 2011.03.24 242
193 Function.apply and Function.call in JavaScript 황제낙엽 2011.10.07 238
192 브라우저에서 뒤로 가기 막기와 펑션키(function key) 막기 황제낙엽 2005.10.21 236
191 두 서버의 자원을 접근하는 클라이언트 프레임웍(Next.js)에서의 CORS오류 file 황제낙엽 2021.12.05 231
190 JavaScript Closures for Dummies 황제낙엽 2009.04.08 227
189 Javascript ArrayBuffer ? Binary handling in javascript 황제낙엽 2012.03.19 218
188 javascript array contains method 황제낙엽 2011.08.19 210
187 char to hex string 황제낙엽 2011.11.29 206
186 How to use Rhino to script Java classes. 황제낙엽 2008.07.14 198
185 Page Refresh/Reload 황제낙엽 2007.08.24 197
184 [펌] 아사페릴의 사생활 - Code Conventions for the JavaScript Programming Language 황제낙엽 2009.04.02 194
183 Reference Count (순환참조) 황제낙엽 2011.11.24 191
182 JavaScript Form Validation file 황제낙엽 2008.11.24 186
181 code compressor & decompressor 황제낙엽 2015.01.02 181
180 자바스크립트의 데이터 타입과 변수 황제낙엽 2008.08.06 179
179 SelectBox 밑에 CheckBox가 포함된 리스트 만들기 file 황제낙엽 2007.01.16 178
178 서비스 워커 file 황제낙엽 2020.05.25 177