-1

I want to access array arrcolumn[] which I declared as global array.I am initializing value to array arrcolumn[] in OnSuccess1() function .I want to use arrcolumn[] values in OnSuccess() function after initializing in OnSuccess1(). But arrcolumn[] is empty in OnSuccess() function.

var arrname = [];
var arrmark = [];
var arrcolumn = [];
var arr = [];
var arr1 = [];
//display function
function display() {
    $.ajax({
        type: "POST",
        url: "Default.aspx/fetchStudent",
        data: '{}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        error: OnErrorCall
    });
    $.ajax({
        type: "POST",
        url: "Default.aspx/fetchcolumn",
        data: '{}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: OnSuccess1,
        error: OnErrorCall1
    });

    function OnSuccess1(response) {

        var objdata1 = (response.d);
        var pm1 = JSON.parse(objdata1);
        var len1 = objdata1.length;
        arr1 = $.map(pm1, function(n, i) {
            var arr_temp1 = {
                0: n.name


            }
            arrcolumn[i] = n.name;


            return arr_temp1;

        });



        arrcolumn = jQuery.grep(arrcolumn, function(value) {
            return value != removeItem;

        });
        arrcolumn = jQuery.grep(arrcolumn, function(value) {
            return value != removeItem1;

        });

        alert(arrcolumn);
    }

    function OnErrorCall1(response) {
        alert("unable to fecth");
    }

    function OnSuccess(response) {

        var objdata = (response.d);
        var pm = JSON.parse(objdata);
        var len = objdata.length;
        arr = $.map(pm, function(n, i) {
            var arr_temp = {
                0: n.name1,
                1: n.os,
                2: n.cn,
                3: n.pns,
                4: n.dbms,
                5: n.se,
                6: n.c

            }
            arrname[i] = n.name1;
            arrmark[i] = [n.os, n.cn, n.pns, n.dbms, n.se, n.c];

            return arr_temp;

        });
        //alert(arrcolumn);
        alert(arrname);
        alert(arrmark);
    }

    function OnErrorCall(response) {

        alert("error occured");
    }
}
Adam Azad
  • 11,171
  • 5
  • 29
  • 70
user6885473
  • 298
  • 1
  • 5
  • 20
  • This code and question are really difficult to understand, especially since you use such similar condensed variable names. What are you trying to achieve? Can you give a simple, formatted example of what works and what doesn't? – moopet Jan 10 '17 at 08:53
  • 1
    Maybe your `OnSuccess1` is executed before `OnSuccess`? Ajax is async and will take variable amount of time to complete – Justinas Jan 10 '17 at 08:54
  • 1
    You can't guarantee which will finish first, make them in series – Dominic Jan 10 '17 at 08:55

1 Answers1

1

You have no guarantee that OnSuccess1 will be called after OnSuccess. If you need both pieces of information before you can do the work, you need to wait for both calls to complete.

One easy way to do that is jQuery's $.when:

$.when(
    $.ajax(/*...*/), // The first call
    $.ajax(/*...*/)  // The second call
).then(function(result1, result2) {
    // Both calls are done now. Use `result1` (the result of the
    // first call) and `result2` (the result of the second) here
});

The calls will run in parallel, but the handler at the end won't be run until both calls complete.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • *(Doh! If you see "before" rather than "after" in the first sentence, hit refresh.)* – T.J. Crowder Jan 10 '17 at 08:55
  • Why didn't you dupehammer this question to [How do I return the response from an asynchronous call?](http://stackoverflow.com/q/14220321/3853934) – Michał Perłakowski Jan 10 '17 at 08:57
  • @Gothdo: I don't think it's a dupe of that. It may well be a dupe of something else to which `$.when` or `Promise.all` is the answer, but the OP was clearly waiting for the callbacks before doing their work. They just didn't realize the callbacks may not be in order. I got through the first three pages on [this search](/search?tab=relevance&q=%5bjquery%5d%20wait%20for%20multiple%20ajax%20complete) and gave up. – T.J. Crowder Jan 10 '17 at 08:58