Reputation: 85
How I can set "price" variable after ajax call? here the code:
var price;
var idProd = data.prodotto_id;
var cb = function (o,s,r){
price = Ext.util.JSON.decode(r.responseText).price;
console.log(price);
};
Ext.Ajax.request({
url : "index.php",
params : {
m : "Prodotti",
a : 'prod-price-byquantity',
idProd : idProd,
quantity: qta
},
callback : cb,
scope : this
});
console.log(price);
In the last console.log(price) I see price as undefined
Upvotes: 0
Views: 3787
Reputation: 11
this work
fetchRow:function(params){
var me = this;
var v;
this.fetchRowRequest(params,function(value){
v = value;
});
return v;
}
fetchRowRequest: function(params,callback){
var me = this;
var record;
Ext.Ajax.request({
url: me.proxy.api.read,
params: {filters:Ext.encode(params)},
async:false,
success: function(response){
var response = Ext.decode(response.responseText);
var row = response.rows[0];
if(row){
var record = Ext.create(me.model.prototype.modelName);
record.set(row);
}else{
record = false;
}
callback(record);
}
});
}
Upvotes: 1
Reputation: 4059
As you can see,"price" variable is assigned a value inside "cb" function so price is accessible only inside cb.You must use this.price="bla bla" so price will be available and modifiable globally inside your class/namespace.
Then you must use console.log(this.price); to output the price.
Upvotes: -1
Reputation: 13927
That's because Ajax requests are asynchronous - your callback function will not be called immediately. Here's how it goes:
var price; // = undefined;
Ext.Ajax.request();
// The request is sent and the function immediately returns
console.log(price); // undefined
...
some time passes
...
// Finally the request finishes and your callback function is called;
price = Ext.util.JSON.decode(r.responseText).price;
console.log(price); // some new value
So you are getting the price variable after the ajax call has finished.
Upvotes: 1