您似乎正在记录 jqHXR 对象,这可能是 jQuery.ajax() 和 jQuery.when() 交互以提供结果 see last but one example here 的奇怪方式的结果。
由于预期在稍后阶段清除 jQuery,我冒昧地建议您在此阶段需要标准化所有调用。这相当简单,只需使用.then() 而不是.done() 并期望结果以数组形式传递:
getAll(`/response/${customerId}`, `/response/${methods}`)
.then(results => {
console.log("getAll0", results[0]);
console.log("getAll1", results[1]);
});
然后,您需要在 getAll() 中跳几圈,以便标准化 jQuery.ajax() 和 jQuery.when() 行为的各个方面:
- 将 jQuery.when() 的传递结果标准化为单个参数而不是数组。
- 将 jQuery.ajax() 的(data、statusText、jqXHR)交付标准化到其成功路径。
- 标准化 jQuery.ajax() 将 (jqXHR, statusText, errorThrown) 传递到其错误路径。
- 标准化
jQuery.ajax() 的错误处理程序在不同版本的jQuery 中的行为。
export const getAll = (...ajaxCalls) => {
function makeAjaxCallAndStandardizeResponse(call) {
return $.ajax(call)
.then(
// success handler: standardize success params
(data, statusText, jqXHR) => data, // discard statusText and jqXHR
// error handler: standardize error params and ensure the error does not remain "caught".
(jqXHR, textStatus, errorThrown) => $.Deferred().reject(new Error(textStatus || errorThrown)).promise(); // discard jqXHR, and deliver Error object on the error path.
);
}
// Aggregate with Promise.all() instead of jQuery.when() to cause a javascript Promise to be returned and results to be delivered as Array.
return Promise.all(ajaxCalls.map(makeAjaxCallAndStandardizeResponse));
}
成功处理程序可能是不必要的,因为 Promise.all() 聚合会自动导致 statusText 和 jqXHR 被丢弃,但明确这些丢弃并没有真正的危害,除非您需要关注毫秒。
从错误处理程序返回$.Deferred().reject(new Error(textStatus || errorThrown)).promise() 应该在所有版本的jQuery 中给出相同的行为,从而导致错误路径上传递一个错误对象。 (但记录的错误消息会有所不同)请务必对此进行测试。