How to grab return value from an ajax call?
It's asynchronous, so you have to wait for the ajax call to get the data back before you can alert it. You can do that easily by returning the ajax call and using done()
, like so:
$(function() {
$('#add_product').click(function() {
var i = $('#product_name').val(),
par = 'product_name=' + i;
check_product(par).done(function(value) {
alert(value); //waits until ajax is completed
});
return false;
});
});
function check_product(param) {
return $.ajax({
type : 'POST',
data : param,
url : baseurl + 'cart/check_product_name/'
});
}