SET VALUE OUT OF MOOTOOLS REQUEST

Alex Leblois

Total Post:67

Points:471
Posted by  Alex Leblois
JSON 
 1111  View(s)
Ratings:
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;
            }
        }
    });
    request.send();
    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. Sumit Kesarwani

    Post:378

    Points:2694
    Re: set value out of mootools request

    Hi,

    set asynchronous request to false as following

    async: false,

    I hope now it's working.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!