2010-05-28 2 views
4

내 Ajax 호출이 성공적인 결과를 반환하지만 예외가 발생하는 결과를 처리하는 동안 오류 처리기가 발생합니다. 이것은 Ajax 호출을하거나 서버 측 오류를 통해 오류가 발생하는 경우에만 오류 처리기를 발생시켜야한다고 생각하기 때문에 나에게 직관적 인 것처럼 보입니다. 단위 테스트에서 Ajax 함수를 사용하려고합니다. 그래서 두 가지 다른 실패 시나리오의 차이점을 말씀 드리고 싶습니다.jQuery 오류 처리기를 우회 할 방법이 있습니까?

+0

흠, 나는 그것을 결코 알지 못했다. 아마 AJAX 프로세스를 디자인보다는 jQuery로 내부에서 처리했기 때문에 가능하지만 여전히 수정해야 할 부분입니다. – Matchu

답변

1

나는이 완전히 잘못 해석하고있어 경우에 저를 용서하지만이처럼 사용할 수있는 .ajaxError() handler, 특별히 찾고있는 것 같다

$(document).ajaxError(function(event, xmlHttp, options, error) { 
    alert(error); 
}); 

또는 당신이 그것을 바인딩 할 수 있습니다, 그것은이 ajaxError 이벤트입니다 이는 click 이벤트와 같습니다. 이것은 모든 jQuery throw 대신 AJAX 오류에만 해당합니다.

0

방금 ​​아래 테스트를 실행했습니다. HTTP 200이 다시 전달되면 오류가 발생하지 않습니다. var '결과'에는 예상 한 내용이 포함됩니다. JSON을 사용하지 않으려면 데이터 유형을 제거하십시오.

 
function ajaxRequest(request, url) { 
    $.ajax({ 
    type: "POST", 
    contentType: "application/json; charset=utf-8", 
    url: url, 
    data: data, 
    dataType: "json", 
    cache: false, 
    async: false, 
    success: function(result) { 
     //this only happens on success 
    }, 
    error: function(msg,XMLStatus,Err) {  
     ajaxError(msg,XMLStatus,Err); //Call generic error message 
    } 
    }); 
} 

웹 서비스 상호 작용의 일반적인 성공/오류 메서드로 사용하는 경향이 있습니다.

 

/* Data must be prepared in a standard JSON format, either using $.toJSON() for objects, or stringbuilding for individual parameters */ 

/* General AJAX request handler */ 
/* serviceName is the full path of the service ie. "fullpath/services/service.asmx/method/" */ 

function ajaxRequest(data, serviceName, callbackFunction, async) { 
    $.ajax({ 
    type: "POST", 
    contentType: "application/json; charset=utf-8", 
    url: serviceName, 
    data: data, 
    dataType: "json", 
    cache: false, 
    async: async, 
    success: function(response) { 
     callbackFunction.Success(response); 
    }, 
    error: function(msg,XMLStatus,Err) {  
     callbackFunction.Error(msg,XMLStatus,Err); 
    } 
    }); 
} 

/* Sample use */ 
function getStuff(a,b,c){ 
    //Sample method signiture: getStuff(string a, string b, string c) 
    var data = "{" 
    data += "'a'" + ":'" + a + "'" 
    data += ",'b'" + ":'" + b + "'" 
    data += ",'c'" + ":'" + c + "'" 
    data += "}"; 
    someParameterImayWantToUseInTheCallBack = "This was the test click button"; 
    serviceName = "/taccapps/samples/servicesample.asmx/getStuff/"; 
    ajaxRequest(data, serviceName, new sampleCallback(someParameterImayWantToUseInTheCallBack), true); 
} 

/* Sample callback */ 
function sampleCallback(someParameterImayWantToUseInTheCallBack){ 
    //someParameterImayWantToUseInTheCallBack is available to both the success and fail methods 
    this.Success(response){ 
    //"response" represents the JSON response from the server. If it is a list/array it can be dotted into using the index. 
    for(item in response){ 
     alert(response[item].a); 
     alert(response[item].b); 
     alert(response[item].c); 
    }  
    }; 
    this.Error(msg,XMLStatus,err){ 
    //Standard HTTP error codes are found in the above 
    }; 
} 
관련 문제