我编写了一个简单的通用 ajax 函数,可以在我的脚本中被多个函数调用。我不确定如何将返回给 ajax 函数的数据返回给调用者。
// some function that needs ajax data
function myFunction(invoice) {
// pass the invoice data to the ajax function
var result = doAjaxRequest(invoice, 'invoice');
console.dir(result); // this shows `undefined`
}
// build generic ajax request object
function doAjaxRequest(data, task) {
var myurl = 'http://someurl';
$.ajax({
url: myurl + '?task=' + task,
data: data,
type: 'POST',
success: function(data) {
console.dir(data); // this shows good data as expected
return data; // this never gets back to the calling function
}
});
}
有没有办法将ajax数据返回给调用函数?