jQuery AJAX 호출에서 '최종'과 유사한 것이 있습니까?
jQuery AJAX 호출에 Java '마지막'아날로그가 있습니까? 여기에이 코드가 있습니다. 내 항상 예외를 던지지 만 항상 then () 메서드 로 이동하기를 원합니다 .
call.xmlHttpReq = $.ajax({
url : url,
dataType : 'json',
type : 'GET'
}).always(function(processedDataOrXHRWrapper, textStatus, xhrWrapperOrErrorThrown) {
throw "something";
}).then(function() {
alert("i want to always run no matter what");
});
done () , complete () 및 다른 always () 를 사용하려고 시도 했지만 아무것도 작동하지 않는 것 같습니다.
다음은 JSFiddle입니다.
이 예를 참조하십시오.
$.ajax({
type: "GET",
dataType: dataType,
contentType: contentType,
async: TRUE,
url: $('html form:nth-child(1)').attr('action') + "?" $('html form:nth-child(1)').serialize(),
success: function(data) {
console.log("FUNFOU!");
},
error: function(data) {
console.log("NÃO FUNFOU!");
},
complete: function(data) {
console.log("SEMPRE FUNFA!");
//A function to be called when the request finishes
// (after success and error callbacks are executed).
}
});
자세한 정보 : http://api.jquery.com/jquery.ajax/
.always()작동해야합니다. http://api.jquery.com/jQuery.ajax/ 의 The jqXHR Object 섹션을 참조하십시오 .
jqXHR.always (function (data | jqXHR, textStatus, jqXHR | errorThrown) {}); 완전한 콜백 옵션에 대한 대체 구조 인 .always () 메서드는 더 이상 사용되지 않는 .complete () 메서드를 대체합니다.
성공적인 요청에 대한 응답으로 함수의 인수는 .done ()의 인수와 동일합니다 : data, textStatus 및 jqXHR 객체. 실패한 요청의 경우 인수는 jqXHR 객체, textStatus 및 errorThrown과 같은 .fail ()의 인수와 동일합니다. 구현 세부 사항은 deferred.always ()를 참조하십시오.
http://api.jquery.com/deferred.always/ 도 참조 하십시오.
아래 제안은 jQuery에서 작동하지 않습니다. jQuery의 promise 구현은 그때 전달 된 메서드에서 발생한 오류를 처리하지 않기 때문입니다. jQuery가 promises / A +를 준수하는 경우 가능할 수있는 일의 예시로 여기에 남겨 두겠습니다. Bergi가 올바르게 지적했듯이 코드를 직접 try catch 블록으로 래핑해야합니다.
call.xmlHttpReq = $.ajax({
url : url,
dataType : 'json',
type : 'GET'
}).then(function(processedDataOrXHRWrapper, textStatus, xhrWrapperOrErrorThrown) {
throw "something";
}).always(function() {
alert("i want to always run no matter what");
});
jquery의 약속이 항상 지원하는지 확실하지 않지만 대안은 then (다시)을 사용하고 다음과 같이 successHandler 및 errorHandler와 동일한 함수를 전달하는 것입니다.
call.xmlHttpReq = $.ajax({
url : url,
dataType : 'json',
type : 'GET'
}).then(function(processedDataOrXHRWrapper, textStatus, xhrWrapperOrErrorThrown) {
throw "something";
}).then(function() {
alert("i want to always run no matter what");
},
function() {
alert("i want to always run no matter what");
});
jQuery 3.0 이상을 사용하는 사람들을위한 참고 사항
사용 중단 알림 : jqXHR.success (), jqXHR.error () 및 jqXHR.complete () 콜백은 jQuery 3.0부터 제거되었습니다. 대신 jqXHR.done (), jqXHR.fail () 및 jqXHR.always ()를 사용할 수 있습니다.
There is a bug ajax is dependent on the server, need to check status with "complete" is the best, a kind of "success", "error" and others are not 100% of the PUT, POST and GET ... look at an example
$.ajax({
url: '/api/v2/tickets/123456.json',
....
....
....
complete: function(data) {
if (data.statusText == "success") {
console.log("Sent successfully");
} else {
console.log("Not Sent");
}
}
});
Sorry bad english! Cheer ;-)
if you want one code definition for all ajax requests, you can do it like this
$(document).ajaxComplete(function () {
console.log('ajax complete on doc');
})
참고URL : https://stackoverflow.com/questions/15925522/is-there-any-analog-to-a-finally-in-jquery-ajax-calls
'IT TIP' 카테고리의 다른 글
| Groovy에서 조건부 수집을 작성하는 방법은 무엇입니까? (0) | 2020.11.26 |
|---|---|
| powershell을 사용하여 레지스트리 키의 값과 값만 가져 오는 방법 (0) | 2020.11.26 |
| 입력 텍스트 요소에 대한 올바른 읽기 전용 속성 구문은 무엇입니까? (0) | 2020.11.26 |
| Dart 프로그램을 "수면"하려면 어떻게해야합니까? (0) | 2020.11.26 |
| npm 오류! (0) | 2020.11.26 |