how to grab return value from an ajax call?


This question already has an answer here:


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/'

Add this to ajax options:

dataType: "json",

and use

return Json(dataObject, JsonRequestBehavior.AllowGet);

in your action method.


return result;

in the success handler of ajax is not a return for check_product. Pass another function (possibly anonymous) to check_product and call on ajax success.

Need Your Help

How could I ignore bin and obj folders from git repository?

.net git gitignore

I want to ignore bin and obj folders from my git repository. As I've found out, there is no easy way to do this in .gitignore. So, are there any other way? Using clean solution in Visual Studio?

Set Bitmap as background of Text View - Android

java android background bitmap textview

I have a bitmap in memory at run-time. I want to set the background of a text view to this bitmap. I am not able to find any standard procedure for that (not a hacky one).