Show spinner and disable page while waiting for ajax request

I have a project in which I am using MVC C #, with Bootstrap and FontAwesome.

My goal is to show the spinner and disable the page while waiting for an ajax request.

So far, I have successfully completed this task with the following code:

HTML:

<div id='ajax_loader' style="position: fixed; left: 50%; top: 50%; display: none;">
    <img src="~/Images/ajax-loader.gif">
</div>

JS:

function blablabla() {
            //show spinner, disable page
            var modal = $('<div>').dialog({ modal: true });
            modal.dialog('widget').hide();
            $('#ajax_loader').show();

            $.ajax({
                type: "Get",
                url: url,
                success: function (result) {
                   alert("success! " + result);
                },
                error: function(result) {
                    alert("error!" + result);
                },
                complete: function () {
                    //back to normal!
                    $('#ajax_loader').hide();
                    modal.dialog('close');
                }
            });
        }

Now this code works , my page is disabled, and I show the counter. However , this counter is also unavailable, and I do not want this to happen.

How can I prevent this error?

+4
source share
2 answers

So, after a long search, I came up with my solution:

, , , . "_WaitModal"

<div class="modal hide" id="pleaseWaitDialog" data-backdrop="static" data-keyboard="false">
    <div class="modal-header">
        <h1>Please Wait</h1>
    </div>
    <div class="modal-body">
        <div id="ajax_loader">
            <img src="~/Images/ajax-loader.gif" style="display: block; margin-left: auto; margin-right: auto;">
        </div>
    </div>
</div>

@Html.Partial("_WaitModal") , ( ).

, , :

$('#pleaseWaitDialog').modal();

:

$('#pleaseWaitDialog').modal('hide');

, !

+6

.

Html

<button>Click Me</button>
<div class="ajax_loader hidden"><i class="fa fa-spinner fa-spin"></i></div>

CSS

body{
    position:relative;
}
.hidden{
    display:none;
}
.ajax_loader{
    position:absolute;
    width:100%;
    height:100%;
    left:0;
    top:0;
    background:rgba(0,0,0,.5);
}
.ajax_loader i{
    position:absolute;    
    left:50%;
    top:50%;
}

Script

$("button").click(function () {
    $(".hidden").show();
});

+3

All Articles