関数で AJAX リクエストを実行し、(mootools を使用して) リクエストの結果を返したいと考えています。要求と応答は思い通りに機能していますが、変数はまだ 0 です。
ここに私の機能:
function recalcPrice(baseprice, rclass, count){
var newPrice = 0;
var request = new Request.JSON({
method: 'post',
url: 'controller/xhr_request.php',
async: false, //UPDATE: that line solves the problem
data: {
'performaction' : 'calcNumberPrice',
'baseprice' : baseprice,
'class' : class,
'number' : count
},
timeout : 5000,
onComplete: function(response) {
console.log('response=' + response);
if(response.status == 'success'){
console.log('price = ' + response.price);
newPrice = response.price;
}
}
});
request.send();
return newPrice;
}
コンソールは、価格が正しく計算されたことを示していますが、変数newPrice
は変更されていません...
問題は SO で既に知られていますが、この解決策で解決すべきではありませんか? https://stackoverflow.com/a/5541322/1059828