-1
function getvalue(value) {
        document.getElementById('<%= hdnPack.ClientID %>').value = "0";
        var stdPack = 0;
        alert("a");
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "test.aspx/GetStdSellingPack",
            data: "{'Col1':'" + value+ "'}",
            dataType: "json",
            success: function (data) {
                alert("b");
                if (data != null) {

                    $(data).each(function () {
                        var values = data.d;
                        stdPack = values;
                    });
                }
            },
            error: function (result) {
                alert("Error");
            }
        });
        alert("c");
        return stdPack;

    }

Am trying to execute this function Always returning zero , Actually what happening Alert(a) got Fired ,Then Alert (C) fired And Suddenly it goes to Alert(b). The result I would like to get In order, Alert(a) to execute first then Alert(b) followed by alert(c).Pl give me a solution .Thanks in Advance

Praveen
  • 86
  • 1
  • 9
delip raj
  • 33
  • 2
  • look into returning a promise then resolve the promise in the ajax success. Your ajax call is async so your function will return before if acts upon your variable in the success callback – Quince Mar 19 '15 at 12:20

1 Answers1

0

ajax is asynchronous, so it can't guaranty the sequence of code executed. To return a value from function having ajax call check this link

Community
  • 1
  • 1
StateLess
  • 5,344
  • 3
  • 20
  • 29