0

I need to return value from ajax but it filled 0 every time and didn't wait for ajax process finished var itemId=0; as global value

getitemIDbyProductID(productId,getitemIDbyProductID_success);               
                 alert(itemID + "itemgeted")

I did this

  function getitemIDbyProductID(productId, callback) {
            $.ajax({
                type: "POST",
                url: "Cot.aspx/getitemIDbyProductID",
                data: JSON.stringify({ productId: productId }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {
                    var value = 0;
                   value = JSON.parse(result.d);
                    itemID=callback(value)
                  callback(value);
                },
                error: function (msg) {    }
            }); 
        }

        function getitemIDbyProductID_success(total_percentage) {      
              alert(total_percentage +"fds"+itemID);
        }

but it didn't wait the ajax finished and gives me the itemId = undefiend

sara adly
  • 297
  • 1
  • 5
  • 19
  • it is just hopeless trying to get the itemId value :( , what shall I do? – sara adly Apr 12 '16 at 19:20
  • I assumed from the title this was a duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) (I still think it might be) but I have difficulty understanding what your exact issue is, since you don't `return` anything, and you appear to be using a callback, but you use to *set* a value *inside* your Ajax success function (?) instead of passing a value out of it. – apsillers Apr 12 '16 at 19:22

2 Answers2

1

You're successfully setting the value here:

function getitemIDbyProductID_success(total_percentage) {
    itemID = total_percentage;
    alert(total_percentage +"fds"+itemID);
}

But then, in the code which calls this, you're successfully setting it again:

itemID=callback(value)

Since your getitemIDbyProductID_success doesn't return anything, the return value is undefined. So basically you're unsetting itemID immediately after setting it.

Just invoke the callback, don't use its (non-extant) return value:

callback(value);

Additionally, this isn't going to do what you think:

getitemIDbyProductID(productId,getitemIDbyProductID_success);               
alert(itemID + "itemgeted");

Because getitemIDbyProductID performs an asynchronous operation. Even once the above errors are corrected, this error still remains. And this one is a duplicate of a very popular question (with answers far better than I can provide) here.

Community
  • 1
  • 1
David
  • 208,112
  • 36
  • 198
  • 279
0

You can do something like this:

getitemIDbyProductID(productId,function(val){
  itemID = val;
  alert(itemID + "itemgeted");
});

Basically, you have to wait before the itemID gets assigned right value.

Mahesh Chavda
  • 593
  • 3
  • 9