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

36,700

Solution 1

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.

Solution 2

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

Should be using complete() callback instead.

Share:
36,700

Related videos on Youtube

Farhan Zia
Author by

Farhan Zia

Updated on August 28, 2020

Comments

  • Farhan Zia
    Farhan Zia almost 4 years

    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);
    
            }
    
        });
    
    }
    
  • Daniele B
    Daniele B about 12 years
    do you know that you can mark your answer like "correct" thus getting after some of self-answered questions, the self-learner badgs? :)
  • Kamran Ahmed
    Kamran Ahmed almost 11 years
    @Farhan Zia Thanks a lot brother! :)
  • HorseloverFat
    HorseloverFat almost 11 years
    This is wrong. async=false is not deprecated as of jQuery 1.5.
  • chaitu
    chaitu about 9 years
    I have tried this on my code and it works. Thanks !!! But, any idea on how removing async makes it work ?

Related