开发者

jQuery Ajax (beforeSend and complete) working properly on FireFox but not on IE8 and Chrome

I am using jQuery ajax version 1.4.1 in my MVC application (though the issue I am discussing was same with the old jQuery version 3.2.1) as well, to check during customer registration if the username is already registered. As the user clicks on the "Check Availibility" button, I am showing a busy image in place of the check button (actually hiding the check button and showing the image) while checking the availibility on the server and then displaying a message. It is a Sync call (async: false) and I used beforeSend: and complete: to show and hide the busy image and the check button. This thing is working well on Firefox but in IE 8 and Chrome, neither the busy image appear nor the check button hides rather the check button remained pressed as the whole thing has hanged. The available and not available messages appear correctly though. Below is the code:

HTML in a User Control (ascx):

<div id="available">This Username is Available</div>

div id="not_available">This Username is not available</div>

<input id="txtUsername" name="txtUsername" type="text" size="50" />&nbsp;

<button id="check" name="check" type="button">Check Availability</button>

<img id="busy" src="/Content/Images/busy.gif" />

On the top of this user control, I am linking an external javascript file that has the following code:

$(document).ready(function() {

    $('img#busy').hide();
    $('div#available').hide();
    $('div#not_available').hide();

    $("button#check").click(function() {
        var available = checkUsername($("input#txtUsername").val());

        if (available == "1") {
            $("div#available").show();
            $("div#not_available").hide();
        }
        else {
            $("div#available").hide();
            $("div#not_available").show();
        }
    });
});


function checkUsername(username) {

    $.ajax({

        type: "POST",

        url: "/SomeController/SomeAction",

        data: { "id": username },

        timeout: 3000,

        开发者_开发技巧async: false,

        beforeSend: function() {

            $("button#check").hide();

            $("img#busy").show();

        },

        complete: function() {

            $("button#check").show();

            $("img#busy").hide();

        },        

        cache: false,

        success: function(result) {

             return result;

        },

        error: function(error) {

            $("img#busy").hide();

            $("button#check").show();

            alert("Some problems have occured. Please try again later: " + error);

        }

    });

}


I found the answer to my question. It was actually the sync call (async = false) which was making IE mad. I removed that and adjusted the code and everything is working fine now.


Point to Note: async=false is deprecated as of jQuery 1.5

Should be using complete() callback instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜