我有以下代码
function fetchInfo(tableName, tableFields) {
$.ajax({
url: `${baseUrl}/_api/getTableData('${tableName}')/details?$tableFieldsQueried=${tableFields.join(',')}`,
headers: {
Accept: "application/json;odata=verbose"
},
success: function success(res) {
return res
},
eror: function eror(err) {
console.log(JSON.stringify(err));
}
})
}
fetchInfo("test1", ["tfield", "sfield"])
fetchInfo("test2", ["qfield", "rfield"])
fetchInfo("test3", ["ttfield", "dfield"])
如果我使用
fetchInfo("test4", ["efield","hfield"]).then((res) => console.log(res))
我没有得到,因为应该对 ajax 调用进行链接,所以我如何使用相同的函数通过 ajax 调用来获取数据。
有什么办法或者我需要为每个调用重复 4 次相同的 ajax 代码
最佳答案
您必须实际返回 the ajax request object来自你的函数。
function fetchInfo(tableName, tableFields) {
return $.ajax({ // <-- Here
url: `${baseUrl}/_api/getTableData('${tableName}')/details?$tableFieldsQueried=${tableFields.join(',')}`,
headers: {
Accept: "application/json;odata=verbose"
},
success: function success(res) {
return res
},
eror: function eror(err) {
console.log(JSON.stringify(err));
}
});
}
关于javascript - 使用 .then 进行包装在函数中的 ajax 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50388882/