0

First AJAX makes a post to upload file now I just want this same AJAX when submitted to grab the data from second AJAX and post it together basically I want to post the Id back to the database, everything works just do not know how to combine the data together.

Code

 <script type="text/javascript">
        $(document).ready(function () {
            $('#submit').click(function () {
                var data = new FormData();
                var files = $('#fileupload').get(0).files;
                if (files.length > 0) {
                    data.append("UploadedFile", files[0]);
                }
                $.ajax({
                    type: "POST",
                    url: "ControllerHandler.ashx",
                    contentType: false,
                    processData: false,
                    data: data
                });

            });

            $.ajax({
                url: 'SelectType.ashx',
                method: 'Post',
                dataType: 'json', //make sure your service is actually returning json here
                contentType: 'application/json',
                data: '{}',
                success: function (data, status) {

                    $.each(data, function (i, d) {
                        $('#seasontype_select2').append('<option value="' + d.value + '">' + d.label + '</option>');
                    });

                }
            });
        });
    </script>
coder
  • 8,346
  • 16
  • 39
  • 53
Azib Haque
  • 19
  • 5
  • you want to use the result from the first ajax in the second one? – zb22 Jul 26 '18 at 20:30
  • I want to use the result of second ajax into the first ajax of vice versa, I basically just want the first ajax to submit a file and its doing that now i just want the same submit to also post a Id based on selection to the database – Azib Haque Jul 26 '18 at 20:32
  • So if they select something that value needs to get stored on submit – Azib Haque Jul 26 '18 at 20:33
  • I just want to use d.value in the first ajax if that is a better answer @zb22 – Azib Haque Jul 26 '18 at 20:41
  • try $( "#seasontype_select2 option:selected" ).text(); inside the #submit function to get the value from select element – zb22 Jul 26 '18 at 20:43

1 Answers1

0

Ajax requests can be nested.

$.ajax({
  // your Ajax params for this request
  // ...
  success: function(data){  // There only one argument provided here.
    // First callback
    var dataToSendAgain = data.something;

    $.ajax({
      // your Ajax params for this request
      // ...
      data: dataToSendAgain,
      success: function(data){  // There only one argument provided here.
        // Second callback
      }
    });
  }
});
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64