나는 화가 난 이유를 이해할 수 없기 때문에 여러 아약스를 보낼 때 문제가 있을 때 jquery 의 예를 사용하려고했습니다 . 일반적으로 하나 이상의 ajax를 보내려고하면 응답이 나쁩니다.
나는 다음과 같이하고있다.
Workflow.prototype.ajaxWorkflowsPaymentProcessTransitionsAvailable = function (barcodes) {
var workflow = this;
return $.ajax({
url: "api/v1.0/workflows/paymentProcess/transitions/available",
type: "POST",
data: {barcodes: barcodes},
dataType: 'JSON'
});
};
Workflow.prototype.ajaxViewsDocuments = function (fd) {
var workflow = this;
return $.ajax({
url: "api/v1.0/views/documents",
type: "POST",
data: fd,
processData: false,
contentType: false,
dataType: 'JSON'
});
};
$.when(workflow.ajaxViewsDocuments(fd), workflow.ajaxWorkflowsPaymentProcessTransitionsAvailable(barcodes)).done(function(a1, a2){
console.log(a1, a2);
});
그리고 나는 이것을 console.log에 얻었습니다.
그러나 서버 응답 :
서버를 반환하는 동일한 응답을 어떻게 얻을 수 있습니까?
jQuery ajax는 세 가지 결과를 반환합니다. 따라서에서 두 개의 ajax 호출을 사용할 때 콜백에 전달 된 $.when()
각 인수에는 $.when()
두 개의 배열이 전달됩니다. 여기서 각 배열에는 ajax 호출에 대한 세 개의 인수가 있습니다.
따라서 코드에서 다음을 변경하십시오.
console.log(a1, a2);
이에:
console.log(a1[0], a2[0]);
당신이 jQuery를 아약스 호출에서 다시 얻는 세 가지 일반적인 인수에 전달된다 a1[0]
, a1[1]
, a2[2]
. 반환 된 데이터 인수는 [0]
각 배열 의 요소에 있습니다. 여기 에서 문서의 예를 볼 수 있습니다 .
$.ajax()
세 개의 결과 값을 가져 와서 하나의 결과 값으로 잘라낸 다음 $.when()
배열에 넣지 않는 자체 래퍼를 만들 수 있습니다 .
// wrapper function to make return result from $.ajax() be one single argument
$.ajax2 = function() {
return $.ajax.apply($, arguments).then(function(result) {
// return only single result, not all three typical arguments
// so $.when() won't put the args in an array
return result;
});
};
그런 다음 다음과 같이 사용합니다 (에서 $.ajax()
로 변경됨 $.ajax2()
).
Workflow.prototype.ajaxWorkflowsPaymentProcessTransitionsAvailable = function (barcodes) {
var workflow = this;
return $.ajax2({
url: "api/v1.0/workflows/paymentProcess/transitions/available",
type: "POST",
data: {barcodes: barcodes},
dataType: 'JSON'
});
};
Workflow.prototype.ajaxViewsDocuments = function (fd) {
var workflow = this;
return $.ajax2({
url: "api/v1.0/views/documents",
type: "POST",
data: fd,
processData: false,
contentType: false,
dataType: 'JSON'
});
};
$.when(workflow.ajaxViewsDocuments(fd), workflow.ajaxWorkflowsPaymentProcessTransitionsAvailable(barcodes)).done(function (a1, a2) {
console.log(a1, a2);
});
jQuery 1.x, 2.x 및 3.x로 개념을 테스트했습니다. https://jsfiddle.net/jfriend00/2mcLsw3f/
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다