skip to Main Content

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);
        }
    });
}

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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();
        }
        }); 
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search