Scary AJAX error

May 15, 2006 / Filed under: JavaScript, Tips

While doing some AJAX work this morning, I kept getting this error, which looks pretty scary:

Error: [Exception... "Component returned failure code: 0x80040111 (NSERRORNOTAVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111 (NSERRORNOTAVAILABLE)" location: "JS frame :: http://192.168.3.16/liso/javascript/servicerep.js :: checkNameStateChange :: line 50" data: no] Source File: http://192.168.3.16/liso/javascript/servicerep.js Line: 50

Ugly, eh? Yuck.

It was "tripping up" on the line in my JavaScript that began like this:

if (xmlHttp.status == 200)

Now why would it stop there?

Anyone familiar with AJAX would recognize that line as the "check" for data that’s been returned from a remote server call. In other words, if our remote server call returns data, the status would be 200. If not, the status would be 204, which means No Content.

I was positive the remote call returned data, because I checked the query myself.

Turns out my problem had to do with the "trigger" that initially began the AJAX request.

The "trigger" is an HTML form button, which looked like this:

<input type="submit" id="addbutton" value="Add New Account" />

This is just a simple "form submit" button.

However, it doesn’t actually submit the form through the action attribute in the <form> element. Rather, I use the id attribute, "addbutton", to grab the "click" event.

Long story short (I think I already made this too long), I had to change the submit button to this:

<input type="button" id="addbutton" value="Add New Account" />

You see, it was trying to submit the form, at the same time as running the AJAX call, behind the scenes. By changing the <input> element to a button, this resolved the problem.

So, to sum this jibberish up - if you ever see that error - check your "trigger."

Comments/Mentions

# Joshua Benuck at 6/17/2006 1:39 pm cst

I just wanted to thank you for writing this blog entry. It gave me just what I needed to figure out why I was getting the same error. I had an input with an onchanged event within a form and when I removed the form (since it was really unneeded) the error went away.

Thanks again!

# Philip at 6/27/2006 6:32 am cst

why couldn’t you just return false; from your click handler?

# Matthom at 6/27/2006 6:57 am cst

I suppose that would work. Haven’t tried it. Thanks for the idea.

# Harry at 10/4/2006 12:20 pm cst

Damn, this was the solution i was looking for. I had a problem in Firefox that uses an image button (ASP.NET) and once I changed the image button to a regular input type=button, it worked like a charm. Only thing is, it's going to look out of place when all the other buttons in the application uses image buttons. Any suggestions?

# Md Abdul Quayum at 11/18/2006 6:12 am cst

I was triying to solve this problem for 1 week, when srinivas give me this link in a group. It worked for me.
Thanks alot.
I am using the same AJAX to call a method on the server which sends a mail to the user.

my code if(status == 200) is evaluating to false yet the mail is
delivered. But because of the above status I am un able to display the
message to the user. Do any one hava an idea how to access
responseText or ResposeXML without status=200 because my prime target
is to display messages in the user interface.

I am returning action messages from my class, these action messages
need to be displayed in the user intrface. I am using spanElements and
replaceExistingElements to show these messages.
The problem is that the "req.status" is not becoming 200 in the
following code:

if (status == "ok" || status == "OK" || status == 200) { // OK response
reached now we can process the response
//Split the text response into Span elements
spanElements =
splitTextIntoSpan(req.responseText);
//Use these span elements to update the page
replaceExistingWithNewHtml(spanElements);
}
else {
alert("Problem with browser response:n Response status "
req.statusText);
}

so, I am getting the alert even when the email is actually send.

alert("the length of returned text is "
req.responseText.length );
is 15178 where as
alert("responseXML " req.responseXML); is null

please suggest me how to solve this problem.

thanks:

# Matthom at 11/18/2006 7:15 am cst

Md Abdul Quayum, I don't know why the status sometimes doesn't come across as 200, even though, as you said, it's clearly processing the server instructions just fine.

I have come across this many times. It is quite frustrating as I don't have a solution to it yet...

# Md Abdul Quayum at 11/19/2006 7:59 pm cst

Dear Matthom,
I agree with you taht this is really frustrating that one do not get a result out of correct code. At the same time it is difficult to get the work done, because the same come is absolutely working fine for other module.
I am triying to intentionally put some errors in the code and diagnose the result. How far am I right.

# A.S. at 12/30/2006 5:41 am cst

I get different results:

When I call the ajax function from outside the form bounds via a

# A.S. at 12/30/2006 5:44 am cst

I get different results: (sorry for the earlier entry - unintentional html re the button.)

When I call the ajax function from outside the form bounds via a button onClick, works OK.

When I call that function from inside - also via a button onClick ... - I get the error. The browser is invoking the form's action and method, cuz I put a crazy value in for the action, and it 404's on that value.

Any thoughts really, really appreciated.

# Matthom at 12/30/2006 6:15 am cst

A.S., what type of <input> element are you using?

Is it type="submit", or type="button"?

If it's submit, obviously it will try to use the form's action value.

Does that help?

# Md Abdul Quayum at 1/5/2007 8:19 pm cst

A.S., what type of <input> element are you using?

Is it type="submit", or type="button"?

Using type="button" worked out for me.

# Kavorka at 1/15/2007 12:19 am cst

thanks for your blog entry. in my case I was triggering from link ; I changed it to href="#" and it began to work.

# adam at 5/30/2007 10:51 pm cst

you're a hero dude

# Sravanthi at 6/4/2007 9:37 pm cst

Many thanks. Really I struggled for this error two days. But with your simple solution made me free. Thanks.

# paul at 7/4/2007 4:07 am cst

saved the day - an instance where firefox fails but ie works - was driving me mad

# jpatiani at 12/10/2007 3:08 am cst

wow, that very releasing. thanx, now my program work fine. the problem only change type from "submit" to "button". thanx

# Ranjan at 1/2/2008 12:44 pm cst

Awesome...saved me hours. Thanks

# Odysseus at 1/8/2008 3:01 am cst

Your post was very helpfull in figuring out my problem. I had the same error, but my setup was a bit different.

I had a popup opened by a parent window. That popup used a callback function to submit something back to its parent window and after that it closed itself. The callback function was defined in the parent window, but was then assigned to a global variable in the popup dialog. So when the function was called, it ran in the scope of the popup dialog, but the AJAX/XMLHTTPRequest handler that it used was in the parent window. (I hope you can still follow me) After the popup dialog closed, the AJAX/XMLHTTPRequest call returned and failed on the status check. This had to do with the fact that the popup dialog wasnt there anymore (when i left the popup open, it did not fail). I fixed it by placing the code that did the AJAX-XMLHTTPRequest inside a setTimeout(). I think this broke the references to the scope of the popup dialog and placed it back in the parent window. After this fix the status check did not fail anymore.

# Peter at 2/11/2008 7:49 pm cst

Hi, I am getting the same error (I google you with that error text), but it is different problem since I don't use button to start XMLHttpRequest, but onload event to start a recursive function. So I make new XMLHttpRequest every one minute (to check new mails) via setTimeout().

Anyway... the error show up only occasionally. That means, request run just ok for example 20 times and then crash on that error, at the same line...

if (http_request.status==200) {...

I am not sure if this is not just Firefox problem, but if anyone have some information, please let me know.

# Odysseus at 2/12/2008 12:18 am cst

Peter,

does your error appear when you reload the page? If not can you post some more code?

# Peter at 2/12/2008 4:27 am cst

Reloading the page fix the problem, but as I said, sometimes that error show up again.

code: function doRequest(k) { if(!titulka) getTitle(); document.title = '+ ' + titulka; // debug

var url = 'http://www.bombakseft.sk/ajax/posta.php;
var http_request = false;

if (window.XMLHttpRequest) {
    http_request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
    try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
    }
}

if (!http_request) return false;
http_request.onreadystatechange = function() { spracuj(http_request, k); };
http_request.open('GET',url,true);
http_request.send(null);

}

function spracuj(httprequest, k) { if (httprequest.readyState==4) { if ((httprequest.status==200) || (httprequest.status==0)) { var pocet = httprequest.responseText * 1; if (pocet != 0) { document.title = '['+pocet+'] '+ titulka; document.getElementById('item06').className = 'active'; doPlay(); } else { setTitle(); document.getElementById('item06').className = ''; } } else { alert('ERROR, status: '+httprequest.status); } setTimeout('doRequest('+k+')', 60000); } }

note: There is some other functions and global variables declared out of this two functions, you can discover, but they are not involved in problem to be sure.

# Peter at 2/12/2008 4:31 am cst

Oh... this is really mess comment html formating on this site, you should fix it dude.

# Odysseus at 2/12/2008 6:47 am cst

Hmm cant seem to find the reason for it. Maybe its just a glitch in the browser or something.

# Bal at 2/18/2008 5:59 pm cst

Thanks for the post! it would have taken me days, if ever, to figure this out

# Kliment Stefanov at 4/3/2008 6:59 am cst

Another option is to use Synchronous XMLHttpRequest, which will preform both operations one after another.

Cheers,

Pimmy

# BHJ at 5/22/2008 4:17 am cst

Cheers for the blog entry, i had a trigger prob too where i was making 2 ajax calls which were triggering the same thing! This entry helped me lots!

# Thomas Guymer at 6/2/2008 7:33 am cst

Thanks for the post - very helpful!

# Al Sweigart at 12/1/2009 10:24 am cst

I've discovered the source of this error.

When non-IE web browsers begin to submit a form, they first kill any currently existing AJAX requests. (This makes sense, since the page will reload soon (except in the case of very long file uploads from the form, which is where I encountered this problem.)) So the query will otherwise work and any backend code will run, but the AJAX request itself will return a code of 0 and a blank string for content.

The solution is use a setTimeout call to kick off the AJAX request for the submit button's onclick. This way, the AJAX request starts after the form has been submitted.