Developer
순수 javascript cross-browser ajax 구현 (Xmlhttprequest)
Angeleyes
2012. 7. 25. 16:54
jquery로 구현하게 되면 매우 간단하나.. 오늘은 샘플로 올려 놓고자 합니다..
아래와 같이 구현하시면 될 것 같습니다.
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
alert(xmlhttp.responseText);
}
else{
//alert(xmlhttp.readyState);
//alert(xmlhttp.status);
}
/*
onreadystatechange = Stores a function (or the name of a function) to be called automatically each time the readyState property changes
readyState = Holds the status of the XMLHttpRequest. Changes from 0 to 4:
0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is ready
status
200: "OK"
404: Page not found
*/
}
xmlhttp.open("GET","http://blog.angeleyes.kr/312",true);
xmlhttp.send();
onreadystatechange에서 else문으로 error를 잡으면 될 것 같습니다.
그리고 cross domain 문제의 경우에는 proxy를 이용한 방법으로 처리하는 것이 좋을 것 같습니다.
proxy 참고 : http://renewal.tistory.com/114
cross domain ajax 참고
http://greatkim91.tistory.com/107
감사합니다.