내 Ajax 호출이 성공적인 결과를 반환하지만 예외가 발생하는 결과를 처리하는 동안 오류 처리기가 발생합니다. 이것은 Ajax 호출을하거나 서버 측 오류를 통해 오류가 발생하는 경우에만 오류 처리기를 발생시켜야한다고 생각하기 때문에 나에게 직관적 인 것처럼 보입니다. 단위 테스트에서 Ajax 함수를 사용하려고합니다. 그래서 두 가지 다른 실패 시나리오의 차이점을 말씀 드리고 싶습니다.jQuery 오류 처리기를 우회 할 방법이 있습니까?
4
A
답변
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 }; }
관련 문제
- 1. 진정한 오류 처리기를 구축하십시오.
- 2. MS 우회 API 함수를 우회하는 우회 도서관
- 3. IE의 XSS - 우회 방법?
- 4. 임시 우회 ListView 스크롤
- 5. 외래 키 - 일시적으로 우회?
- 6. 외부 JS 파일이 동일한 출처 정책을 우회 할 수 있습니까?
- 7. 지정한 시간이 지나면 std :: cin을 우회 할 수 있습니까?
- 8. cURL을 사용하여 서버 보안을 우회 할 수 있습니까?
- 9. iphone sdk에서 UIImagePicker를 우회
- 10. 스푸핑 리디렉션을 우회
- 11. grails보기 해결자를 우회 하시겠습니까?
- 12. .NET 3.5 Compact Framework에서 전역 오류 처리기를 사용할 수 있습니까?
- 13. 일련의 jQuery AJAX 호출을 수행 할 수있는 방법이 있습니까?
- 14. jQuery 컨텍스트를 공유하는 방법이 있습니까?
- 15. 스위치 "제어 전송의 초기화를 우회 :"나는이 '컨트롤의 전송이 초기화 우회 "얻을
- 16. 가상 함수 후크/우회
- 17. CodeIgniter에 알맞은 오류 처리기를 만들려면 어떻게해야합니까?
- 18. jQuery Mobile에서 listview ('refresh')에 이벤트 처리기를 연결할 수 있습니까?
- 19. 이벤트 처리기를 여러 요소에 바인딩 jQuery?
- 20. 이동중인 Tkinter 창에 이벤트 처리기를 바인딩 할 여지가 있습니까?
- 21. 레일 3 - 자신의 오류 처리기를 만드는 방법
- 22. 오류 처리기에 대한 JQuery 오류 속성
- 23. 범위를 잃지 않고 이벤트 처리기를 추가하고 분리 할 수있는 방법이 있습니까?
- 24. URL 처리를 위해 처리기를 어떻게 할당 할 수 있습니까?
- 25. 콘솔 앱에서 ASP.net 페이지 처리기를 호출 할 수 있습니까?
- 26. bison/flex에서 오류 메시지를 현지화하는 방법이 있습니까?
- 27. 보조 개발 용 번들을 우회
- 28. Perl에서 은닉 할 방법이 있습니까?
- 29. appendTohead에 추가 할 방법이 있습니까?
- 30. jQuery : 이벤트 핸들러를 "새로 고치는"방법이 있습니까?
흠, 나는 그것을 결코 알지 못했다. 아마 AJAX 프로세스를 디자인보다는 jQuery로 내부에서 처리했기 때문에 가능하지만 여전히 수정해야 할 부분입니다. – Matchu