Fixing Firefox’s ‘NS_ERROR_NOT_AVAILABLE’ Error When Using PageMethods.

By | June 18, 2008

So here I am, doing some Virtual Earth stuff with PageMethods (which will be in an upcoming post), something I’ve done many times over the last year and some, and I’m running into some issues.

First off, IE7 does nothing, it just sits there. I add some break points in the static method that the PageMethod is calling, and again, nothing. So I load up Firefox, poynt to the URL of the page, and unlike IE7, Firefox does something.

However, it wasn’t something that I was expecting. In the Error Console it had the following message:

Error: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]” nsresult: “0×80040111 (NS_ERROR_NOT_AVAILABLE)” location: “JS frame :: http://localhost:50007/NS_ERROR_NOT_AVAILABLE/ScriptResource.axd?d=ZgyDU9i45kjT :: Sys$Net$XMLHttpExecutor$get_statusCode :: line 4166″ data: no]
Source File: http://localhost:50007/NS_ERROR_NOT_AVAILABLE/ScriptResource.axd?d=ZgyDU9i45kjT-145205
Line: 4166

Not the most user friendly error message I’ve ever seen. Not knowing what this meant, I decided to search Google for an answer. Well, not a whole lot of luck there.

I did find one semi useful post in some forum though. Apparently, there’s a bug in Firefox that has something to do with Firefox trying to do two calls at once, which got me thinking … What would cause Firefox to make two calls when I click on a button? Hmmz.

Before I show you the solution, I’ll show you something similar to the code that I was using to create the error.

Here’s the HTML for my page:

    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" 
                           enablepagemethods="true" 
                           runat="server" />
        <span id="lblOutput" runat="server" />
        <asp:button id="btnSubmit" text="Click Me!" 
                    onclientclick="btnSubmitClick()" 
                    runat="server" />
    </form>

Nothing too fancy there. A ScriptManager with EnablePageMethods set to true. A span tag for the output from the method that I’m attempting to call. And a button to fire the event, with the OnClientClick set to the name of the JavaScript method to call.

Here’s the JavaScript:

    <script type="text/javascript">
        var rslt = '';
        
        function btnSubmitClick()
        {
            PageMethods.GetHelloWorld(OnGetHelloWorldSuccess, OnGetHelloWorldFailed, rslt);
        }
        
        function OnGetHelloWorldSuccess(rslt)
        {
            var lblOutput = document.getElementById('lblOutput');
            if(lblOutput != null)
            {
                lblOutput.innerHTML = rslt;
            }
            else
            {
                alert('lblOutput is null');
            }
        }
        
        function OnGetHelloWorldFailed()
        {
            var lblOutput = document.getElementById('lblOutput');
            if(lblOutput != null)
            {
                lblOutput.innerHTML = 'Error!';
            }
            else
            {
                alert('lblOutput is null');
            }
        }
    </script>

Again, nothing too fancy. btnSubmitClick() is the method that was specified in the OnClientClick event for the button, which calls PageMethods.GetHelloWorld() . OnGetHelloWorldSuccess() is the method that is called if there are no issues with calling GetHelloWorld(). OnGetHelloWorldFailed() is suppose to be called when there’s a problem when calling GetHelloWorld(). Key words, “suppose to be”.

And lastly, here’s my PageMethod that gets called:

    [WebMethod]
    [ScriptMethod]
    public static string GetHelloWorld()
    {
        return "Bonjour tout le monde!";
    }

Pretty straight forward.

Can you see the problem with this code? Remember, the bug with Firefox is when it tries to do two things at once.

Give up? In the btnSubmitClick() function, just before calling the PageMethod, add alert(‘Click’);. Now, in the code behind, go into the Page_Load event, and add something like bool x = true; and set a break point on it. Now refresh the page, and click on the button.

If you did everything correctly, the alert should popup, and then the break point should be hit. Now do you see where the problem is?

The problem exists because both the PageMethod is being called, and a PostBack is being done, which explains why Firefox was trying to do two things at once.

Here’s the solution to this problem:

#1 Add return false; to the btnSubmitClick() after calling the PageMethod.

        function btnSubmitClick()
        {
            PageMethods.GetHelloWorld(OnGetHelloWorldSuccess, OnGetHelloWorldFailed, rslt);
            return false;
        }

#2 btnSubmit needs to receive the false, so in the onclientclick, before the btnSubmitClick(), add return.

        <asp:button id="btnSubmit" text="Click Me!" 
                    onclientclick="return btnSubmitClick()" 
                    runat="server" />

And there you have it.

If you want to download the code, you can find it here.

8 thoughts on “Fixing Firefox’s ‘NS_ERROR_NOT_AVAILABLE’ Error When Using PageMethods.

  1. mark

    Hi Jay
    Unless you need to use an asp.net control, you can alternatively just use a plain old html input button which won’t have this issue.. That’s what the server will convert it to anyway.

  2. carlj Post author

    Hey Mark, didn’t realize that it was you that posted. :)

    True, I was actually thinking of that the night before you posted. Was talking to Billy about it.

    When I did originally get this error, there wasn’t any solutions as to why the problem was occurring, other than the so-called Firefox bug. So I thought I would post why it’s happening, as well as a fix for it.

    Using a regular HTML input button would be a simpler and better approach than mine.

  3. Martin

    Hi Jay/Mark, I also get this error however as a non technical person where do i fuind the file that needs altering and what do i need to add/alter. I would dearly like to have my firfox back as I don’t like IE?

  4. carlj Post author

    Hi Martin,

    Not too sure I understand.

    This error is caused by a problem in a web page, when two totally different actions happen at the same time when the user performs an action (like clicking on a button).

    This particular problem needs to be fixed in the web page, and not in Firefox.

  5. Beer

    Hi Jay
    Thank you for this solution.
    I have had this issue for a week.

  6. Steve

    Thanks for the tip Carl. Your blog entry was the only good one I could find on Google for this error. Saved me a lot of time!

  7. Marco

    I found your blog, read it and just fixed the bug as if it was nothing. Thx, saved me a lot of time too!
    Greez from Germany!

Leave a Reply

Your email address will not be published. Required fields are marked *