How to refresh page after all ajax calls finish?

Here is my jquery function below. I need to refresh the page so I can display the additional columns after they have checked which columns to display. Currently with the code (location.reload()) it refreshes before the ajax calls can finish. I tried .promise after the checkbox loop but it only allows me to submit 1 checkbox.

Jquery

$('.delete-numbers').click(function () {
        $('.number-chkbox').each(function () {   
            if (this.checked) {
                $(this).attr('checked', true);
                alert($(this).val() + " " + this.checked + "I GOT INNNN ADDDDD");
                $.ajax({
                    url: '/PhoneBook/AddNumber',
                    data: {
                        Number: $(this).val(),
                        Name: name,
                        PhoneId: PhoneId
                    },
                    type: "POST",
                    dataType: "html",
                    cache: false
                });
            } else {
                $(this).removeAttr('checked');
                alert($(this).val() + " " + this.checked + "I GOT INNNN REMOVE");
                $.ajax({
                    url: '/PhoneBook/AddNumber',
                    data: {
                        Number: $(this).val(),
                        Name: name,
                        PhoneId: PhoneId
                    },
                    type: "POST",
                    dataType: "html",
                    cache: false
                });
            }
        })
        location.reload();
    });

Html

<div class="modal-body form-group">
                @foreach (var item in Model.PhoneBook.OrderBy(a => a.Number))
                {
                    if (Model.AvailableNumbers.Any())
                    {
                        if (Model.AvailableNumbers.Where(a => a.Number== item.Number).Count() != 0)
                        {
                            <input class="number-chkbox number" type="checkbox" checked="checked" name="@item.Number_Description" value="@item.Number">
                        }
                        else
                        {
                            <input class="number-chkbox number" type="checkbox" name="@item.Number_Description" value="@item.Number">
                        }
                        <label class="non-bold">@String.Format(" {0} - {1}", @item.Number, @item.Number_Description)</label>
                        <br />
                    }
                    else
                    {
                        <input class="number-chkbox number" type="checkbox" name="@item.Number_Description" value="@item.Number">
                        <label class="non-bold">@String.Format(" {0} - {1}", @item.Number, @item.Number_Description)</label>
                        <br />
                    }
                }
            </div>
<div class="modal-footer">
                    <button type="submit" class="btn btn-danger delete-numbers" data-dismiss="modal">Yes</button>
                    <button type="button" class="btn btn-default cancel-number">No</button>
                </div>

$.ajax({
  ...,
  ...,
  success: function() {location.reload();}
})

JQuery AJAX


I think you should relocate your location.reload() call when an ajaxComplete event occurs.

The easiest way is adding done or success methods into your code. These methods will help you to manage the callbacks when a ajax event occurs.

Using done:

$.ajax({
  url: '/PhoneBook/AddNumber',
  data:
  ...,
  ...,
}).done(function( data ) {
     location.reload();
  });

Also, you can add success:

$.ajax({
  url: '/PhoneBook/AddNumber',
  data: {
  ...,
  ...,
  success: function(data, status, xhr) {
    location.reload();
  }
})

Finally, a more complicated way will be using ajaxComplete events, this is useful Whenever you want to verify if an Ajax request is completed. Therefore, any and all handlers that have been registered with the .ajaxComplete() method are executed at this time.

To observe this method in action, set up a basic Ajax load request:

<div class="delete-numbers">Yes</div>
<div class="result"></div>
<div class="log"></div>

Attach the event handler to the document:

$( document ).ajaxComplete(function() {
  $( ".log" ).text( "Triggered ajaxComplete handler." );
});

Now, make an Ajax request using any jQuery method:

$( ".delete-numbers" ).click(function() {
  $( ".result" ).load( "ajax/test.html" );
});

When the user clicks the element with class delete-numbers and the Ajax request completes, the log message is displayed.

All ajaxComplete handlers are invoked, regardless of what Ajax request was completed. If you must differentiate between the requests, use the parameters passed to the handler. Each time an ajaxComplete handler is executed, it is passed the event object, the XMLHttpRequest object, and the settings object that was used in the creation of the request. For example, you can restrict the callback to only handling events dealing with a particular URL:

$( document ).ajaxComplete(function( event, xhr, settings ) {
  if ( settings.url === "ajax/test.html" ) {
    $( ".log" ).text( "Triggered ajaxComplete handler. The result is " +
      xhr.responseText );
  }
});

Note: You can get the returned Ajax contents just by looking at xhr.responseText.

Now you can show a message when an Ajax request completes.

$( document ).ajaxComplete(function( event,request, settings ) {
  $( "#msg" ).append( "<li>Request Complete.</li>" );
});

If you don't have anything else to do but reload onSuccess, you can put location.reload() under success, else you can put it under the complete function.

$.ajax({
    url: '/PhoneBook/AddNumber',
    data: {
        Number: $(this).val(),
        Name: name,
        PhoneId: PhoneId
    },
    type: "POST",
    dataType: "html",
    cache: false,
    success: function(response){
       ... do what you need here
    },
    complete: function() {
        location.reload();
    }
});