-1

I have a scenario: I am calling a Ajax function A, Once I get the value from A , I wanted to call another Ajax function B with value from A , in the same way I have to call function Ajax function C. Once I got all the result then I have to compile those value to array. I'm not getting how can I do it

And function A has multiple records so I am iterating it. and once I got all the result then compile it for further action.

function A()
{
     $.ajax({
        url: requestUri,
        contentType: "application/json;odata=verbose",
        headers: requestHeaders,
        success: function (data) {
            if (data.d.results.length > 0) {
                $.each(data.d.results, function (index, e) {
                    "Some value"    
                }
            }
        },
        error: function (data) {
            alert(data.responseJSON.error.message.value);
        }
    });
}
function B()
{
     $.ajax({
        url: requestUri,
        contentType: "application/json;odata=verbose",
        headers: requestHeaders,
        success: function (data) {
            Filter based on "Some value" from function A
        },
        error: function (data) {
            alert(data.responseJSON.error.message.value);
        }
    });
}
function C()
{
     $.ajax({
        url: requestUri,
        contentType: "application/json;odata=verbose",
        headers: requestHeaders,
        success: function (data) {
            Filter based on "Some value" from function A
        },
        error: function (data) {
            alert(data.responseJSON.error.message.value);
        }
    });
}
Snow
  • 3,820
  • 3
  • 13
  • 39
Kuldeep Verma
  • 79
  • 2
  • 11
  • for second question. what is the return format of each ajax result. you only can make a object of json if the params are similar. – Ajay Kumar Oad Dec 23 '19 at 05:55
  • or is it just string.? if long string that you want to add to array we can have solution then. tell me so that i can post answer. – Ajay Kumar Oad Dec 23 '19 at 05:56
  • Really not clear if you need a `B()` and `C()` request for every item in initial array from `A()` or not. The best modern approach is to use the promise returned from `$.ajax()` and return that from your function(s). Scenario needs more clarification though – charlietfl Dec 23 '19 at 06:32

2 Answers2

0

You need to call the second function inside the success of the first function. Something like -

function A()
{
     $.ajax({
        url: requestUri,
        contentType: "application/json;odata=verbose",
        headers: requestHeaders,
        success: function (data) {
            if (data.d.results.length > 0) {
                $.each(data.d.results, function (index, e) {
                    B("Some value");//changes here
                }
            }
        },
        error: function (data) {
            alert(data.responseJSON.error.message.value);
        }
    });
}

Note that you would also need to change the declaration of the second function to accept this input.

pinaki
  • 5,393
  • 2
  • 24
  • 32
0

Try Call your B Function inside Your A function and call your C function inside your B function

function A(){

    $.ajax({

    success:function(){
        //your code
        A();
    }
    }); 

}
Hiran
  • 7
  • 2