커피와 개발자

Ajax POST방식과 GET방식 본문

웹개발/자바스크립트(JavaScript)

Ajax POST방식과 GET방식

광박이 2010. 6. 3. 19:07
728x90
function createRequestObject() {
    var reqObj;
    if(window.XMLHttpRequest) {
        try {
            reqObj = new XMLHttpRequest();
        } catch(e) {
            reqObj = false;
        }
    } else if(window.ActiveXObject) {
        try {
            reqObj = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                reqObj = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                reqObj = false;
            }
        }
    }
    return reqObj;
}
var req = createRequestObject();

//POST 방식
function functionPostName(){
var param ="";
param += "Get="+Value;    //Get : 파라메터 변수 Value : 파라메터 값
param += "&Get2="+Value2;
req.open("POST","URL");     //URL : 경로
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.onreadystatechange = resultFunction;
req.send(param);
}
function resultFunction(){
if(req.readyState==4){// 정상일때,
//실행코드
}
}

//GET 방식
function functionGetName(){
var url = "url?Get="+Value+"&Get2="+Value2;
req.open("GET",url,true);
req.onreadystatechange = resultFunction;
req.send();
}
728x90
Comments