Home > other >  how to execute request on javascript that returns value then use this value as parameter for post re
how to execute request on javascript that returns value then use this value as parameter for post re

Time:09-12

I have a two request first one return a value

first request

    $.ajax({
        type: 'POST',
        url: '/controller/action',
        data: JSON.stringify(data),
        contentType: 'application/json',
        success: function (data) {

            result = data.result;


            const add_some_data_at_result = "somdata";}] };

            var obj = JSON.parse(request);
            obj['value'][0].value = editedre; },
        error: function (error) {
            console.log(error);

        }
    })

I want to make second request wait until the first one complete to use its value as a parameter for the second on

second request

              $.ajax({
                type: 'POST',
                url: '/controller/secondAction',
                data: { fits: JSON.stringify(obj) },
                contentType: 'application/json',
                success: function (data) {
                    if (!data.status.includes("Error")) {
                        console.log(data.status);

                        alert('done!');
                        
                    }
                    else {
                        alert('Error');
                    }
                },
                error: function (error) {
                    console.log(error);
                }
            });

any help?

CodePudding user response:

  return new Promise(async function (resolve, reject) {
   $.ajax({
    type: 'POST',
    url: '/controller/action',
    data: JSON.stringify(data),
    contentType: 'application/json',
    success: function (data) {

        result = data.result;


        const add_some_data_at_result = "somdata";}] };

        var obj = JSON.parse(request);
        obj['value'][0].value = editedre; },
    error: function (error) {
        console.log(error);

    }
})
  }).then(
  function (result1) {
        return new Promise(function (resolve, reject) {
            $.ajax({
            type: 'POST',
            url: '/controller/secondAction',
            data: { fits: JSON.stringify(obj) },
            contentType: 'application/json',
            success: function (data) {
                if (!data.status.includes("Error")) {
                    console.log(data.status);

                    alert('done!');
                    
                }
                else {
                    alert('Error');
                }
            },
            error: function (error) {
                console.log(error);
            }
        });
     });
    })
}
  • Related