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:
Ugly, eh? Yuck. It was "tripping up" on the line in my JavaScript that began like this:
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:
This is just a simple "form submit" button. However, it doesn’t actually submit the form through the Long story short (I think I already made this too long), I had to change the submit button to this:
You see, it was trying to submit the form, at the same time as running the AJAX call, behind the scenes. By changing the So, to sum this jibberish up - if you ever see that error - check your "trigger." Comments/Mentions# 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. my code if(status == 200) is evaluating to false yet the mail is I am returning action messages from my class, these action messages if (status == "ok" || status == "OK" || status == 200) { // OK response so, I am getting the alert even when the email is actually send. alert("the length of returned text is " 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, # 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 Is it If it's Does that help? # Md Abdul Quayum at 1/5/2007 8:19 pm cst
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. # 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 wow, that very releasing. thanx, now my program work fine. the problem only change type from "submit" to "button". thanx # 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. 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? Reloading the page fix the problem, but as I said, sometimes that error show up again. code:
} 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. 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! # 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. |
Recent Comments
Recent Music Listens
|
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!