[英]set value out of mootools request
我想在一个函数中执行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
啊,我只是错过了将异步请求设置为false
async: false,
现在正在工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.