user3150977
user3150977

Reputation: 33

Close modal box and show loader before ajax request

On submit of form in modal box, I am calling 2 ajax methods. Everything is fine but I want to show loader and close modal box before ajax methods are called. Below is my code

$("#modalsubmit").click(function () {
    try {
        $("#Modal").modal('hide');
        $("#loader").css("display", "block");
        $.ajaxSetup({
            async: false
        });
        ajaxmethod1();
        ajaxmethod2();
    } finally {
        $.ajaxSetup({
            async: true
        });
    }
    $("#loader").css("display", "none");
});

Here modal box is not closing on click of modalsubmit and loader is also not shown

Strange thing is that if I put breakpoint before ajax than modalbox closes and loader is also shown.. Any help will be appreciated

Upvotes: 0

Views: 538

Answers (1)

jiten jethva
jiten jethva

Reputation: 96

Try this code

<script type="text/javascript">
    $(document).ajaxStart(function () {
        $("#loader").css("display", "block");
    }).ajaxStop(function () {
        $("#loader").css("display", "none");
    }).ajaxComplete(function () {
        $("#loader").css("display", "none");
    }).ajaxError(function (event, xhr, options, exc) {
    });

You can use this for evey ajax call.

Upvotes: 1

Related Questions