我有一个带有两个承诺的javascript函数:
uploadDocument = function (formData, order) {
$.ajax({
type: "POST",
url: "/API/Documents/addDocument",
data: formData,
contentType: false,
processData: false
}).then(function (documentID) {
order.referenceID = documentID;
return $.ajax({
type: "POST",
url: "/API/Documents/addOrder",
data: ko.toJSON(transaction),
contentType: "application/json"
});
}).then(function (result) {
return 'success';
});
}
完美无缺,API称成功。
对该功能的调用是:
uploadDocument(formData, order).then(function (data) {
console.log('success');
})
此时我收到错误:
未捕获的TypeError:无法读取属性'然后'未定义的
我在做什么?
答案 0 :(得分:2)
您需要返回$.ajax()
,然后使用then
。如果没有返回,函数默认返回undefined
,那么为什么会出现错误。在return
之前见$.ajax(...).then(...)
。
uploadDocument = function (formData, order) {
return $.ajax({
type: "POST",
url: "/API/Documents/addDocument",
data: formData,
contentType: false,
processData: false
}).then(function (documentID) {
order.referenceID = documentID;
return $.ajax({
type: "POST",
url: "/API/Documents/addOrder",
data: ko.toJSON(transaction),
contentType: "application/json"
});
}).then(function (result) {
return 'success';
});
}