我知道之前有人问过这件事。我已经多次阅读了这个问题的答案:How do I return the response from an asynchronous call?。我意识到我需要创建一个回调函数来返回结果。我不知道如何按照此处的说明进行操作 - How do I return the response from an asynchronous call? - ,但参数也会传递到函数中(optionSelectionArray)。
function simpleWithAttrPrice(optionSelectionArray){
var product_id= <?=$product_id ?>;
$j.ajax({
type: "POST",
url: "/ajax_calls/childrenToJs.php",
data: { 'productID': product_id, 'optionSelectionArray' : optionSelectionArray}
}).done(function(data) {
return price;
});
}
答案 0 :(得分:3)
您应该使用回调函数调用它(.done(callback)
将调用callback
)而不是像现在这样返回:
function simpleWithAttrPrice(optionSelectionArray, callback) {
var product_id = <?= $product_id ?>;
$j.ajax({
type: "POST",
url: "/ajax_calls/childrenToJs.php",
data: { 'productID': product_id, 'optionSelectionArray' : optionSelectionArray}
}).done(callback);
}
// Usage
simpleWithAttrPrice(options, function(data) {
// Here you have you response
console.log(data);
});