본문 바로가기

Developer/Web

Ajax 오류 메시지 관련...

반응형

요즈음은 페이지의 동적인 전환을 위해서 ajax를 많이 사용하게 됩니다.

jquery 같은 경우에는 간단하게 ajax를 사용할 수 있게 지원하고 있는데요.

순수하게 자바스크립트만을 가지고 ajax를 사용할 경우를 대비하여 까먹기 전에 정리해 놓도록 하겠습니다.

 

 

readyState
0 : 초기화되지 않음.
1 : open 메소드 호출.
2 : open 메소드 호출, send 메소드 호출, 송신완료 요청시작.
3 : 수신중.
4 : 수신완료.

httpRequest.status
200 : 요청성공.
403 : 접근거부.
404 : 페이지 없음.
500 : 서버 오류

500(internal server error)의 경우 오류 내용을 확인하기가 쉽지 않다..
오류내용 확인 방법 :
if (xmlHttpReq.readyState == 4) {
           strResponse = xmlHttpReq.responseText;
           switch (xmlHttpReq.status) {
                   // Page-not-found error
                   case 404:
                           alert('Error: Not Found. The requested URL ' +
                                   strURL + ' could not be found.');
                           break;
                   // Display results in a full window for server-side errors
                   case 500:
                           handleErrFullPage(strResponse);
                           break;
                   default:
                           // Call JS alert for custom error or debug messages
                           if (strResponse.indexOf('Error:') > -1 ||
                                   strResponse.indexOf('Debug:') > -1) {
                                   alert(strResponse);
                           }
                           // Call the desired result function
                           else {
                                   eval(strResultFunc + '(strResponse);');
                           }
                           break;
           }
   }

500번 에러페이지 생성함수
function handleErrFullPage(strIn) {

        var errorWin;

        // Create new window and display error
        try {
                errorWin = window.open('', 'errorWin');
                errorWin.document.body.innerHTML = strIn;
        }
        // If pop-up gets blocked, inform user
        catch(e) {
                alert('An error occurred, but the error message cannot be' +
                        ' displayed because of your browser\'s pop-up blocker.\n' +
                        'Please allow pop-ups from this Web site.');
        }
}


반응형