Advertise with us

set value out of mootools request

Posted by  Alex Leblois
 1729  View(s)
Rate this:
Hi Expert,

I want to perform a AJAX request in a function and then return the result of the request (using mootools). The request and the response are working like I want, but the variable is still 0.

here my function:

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;
    return newPrice;

console shows that the price has calculated correctly, but the variable newPrice is not changed...

The problem is allready known in SO but shouldn't this solution made it?

Any help on above is really appreciated.
  1. Re: set value out of mootools request


    set asynchronous request to false as following

    async: false,

    I hope now it's working.