gpt4 book ai didi

javascript - 嵌套 javascript promise

转载 作者:行者123 更新时间:2023-12-01 03:10:48 25 4
gpt4 key购买 nike

我有一个基于其结果的 AJAX 调用 - 我发送了另一个调用。

uploadDocument = function (doc1, doc2) {
$.ajax({
type: "POST",
url: "/API/UploadDocs/addDocument",
data: doc1,
contentType: "application/json"
}).then(function (result) {
console.log(result);
doc2.id=result;
return $.ajax({
type: "POST",
url: "/API/UploadDocs/addDocument",
data: doc2,
contentType: "application/json"
}).then(function (result) {
});
});
}

但是我收到非法调用错误,我做错了什么?

最佳答案

你错误地执行了 promise 链!当你返回一个 promise 时,你必须继续调用你正在解决的 promise 的 then

阅读链接部分:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

uploadDocument = function (doc1, doc2) {
$.ajax({
type: "POST",
url: "/API/UploadDocs/addDocument",
data: doc1,
contentType: "application/json"
}).then(function (result) {
console.log(result);
doc2.id=result;
return $.ajax({
type: "POST",
url: "/API/UploadDocs/addDocument",
data: doc2,
contentType: "application/json"
});
}).then(function (result) {
//Continue here
});
}

关于javascript - 嵌套 javascript promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45866445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com