Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grab return value from an ajax call? [duplicate]

I wanted to grab the value on an ajax call using a function. but the value always return as undefined. return value is only 1 or 0.

Here is my code:

$(function(){ 
   $('#add_product').click(function(){ 
     var i = $('#product_name').val(); 
     param = 'product_name='+i; 
     var value = check_product(param); 
     alert(value); 
     return false; 
   }); 
});

function check_product(param){ 
  $.ajax({ 
    type : 'POST', 
    data : param, 
    url : baseurl+'cart/check_product_name/', 
    success : function(result){ 
      //alert(result); 
      return result; 
    } 
 });
}

I am still working on getting this one work. I get the value now showing 1 or 0. What im trying to accomplish now is how I can it in the if statement. I wanted to have something like this. If val = 0 return true; else return false. Im not sure I'm in the right track of using the ajax function. But if there a better way you can show me I will appreciate it.

$(function(){ 
       $('#add_product').click(function(){ 
         var i = $('#product_name').val(); 
         param = 'product_name='+i; 
         check_product(param).done(function(value) {
        var val = value; //waits until ajax is completed
         });
         if(val == 0){
            return true;
         } else{
            return false;
         }
       }); 
    });

function check_product(param){ 
  return $.ajax({ 
    type : 'POST', 
    data : param, 
    url : baseurl+'cart/check_product_name/'
 });
}
like image 594
Myke Solidum Avatar asked Nov 30 '12 16:11

Myke Solidum


People also ask

Can we return a value from ajax call?

You can't return "true" until the ajax requests has not finished because it's asynchron as you mentioned. So the function is leaved before the ajax request has finished.

How can I return multiple values from ajax call?

You can only have 1 parameter for your success function: if you need more than 1 value then wrap these values in a server-side object that you stringify with JSON so that you're returning 1 string that you parse back to an object on the client, and from which you can retrieve the different values you need.

How do I return a response from ajax?

The A in Ajax stands for asynchronous. That means sending the request (or rather receiving the response) is taken out of the normal execution flow. In your example, $. ajax returns immediately and the next statement, return result; , is executed before the function you passed as success callback was even called.


2 Answers

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/'
    });
}​
like image 106
adeneo Avatar answered Oct 03 '22 20:10

adeneo


Add this to ajax options:

dataType: "json",

and use

return Json(dataObject, JsonRequestBehavior.AllowGet);

in your action method.

Your

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.

like image 29
Igor Avatar answered Oct 03 '22 19:10

Igor