Thursday, January 26th, 2006

XMLHttpRequest and status code: 0

Category: Ajax, Tip

We run into quirks around the XHR object on various browsers and such. The naive approaches tend to have workarounds put in them, and you soon realise why frameworks like Dojo have the code they do. To make it work.

Marko Samastur wrote about a little quirk, and the magic of status code 0.

Have you ever encountered a problem with XMLHttpRequest (XHR), where its handler couldn’t read request status or the status was set to 0?

Well, I did, a few days ago.

It took me a while to find the cause of this problem and in my case it was form submission. It’s stupid to use AJAX to submit a form, but nevertheless it’s what I’m doing for dubious reasons that I’ll probably be ashamed of in a few months or years. And it used to work fine, until I changed submit buttons from type=�button� to type=�image�.

First type needs javascript to do anything worthwhile, while second type submits form when pressed, which I somehow missed. So, when I thought I was sending only a normal XHR request, I was actually also submitting my form which lead to described strange behavior.

I have no idea why, since XHR state change handler shouldn’t be triggered by other requests, but it was and it was fixed by simply canceling form submission in image button onclick handler.

If you ever encounter this problem, check your forms and their submit buttons. It’s very likely that solution is hiding there.

Posted by Dion Almaer at 7:05 am
13 Comments

+++--
3.5 rating from 53 votes

13 Comments »

Comments feed TrackBack URI

If “unwanted” form submission is the issue, you could have something like onsubmit="return false"> within the form markup – or hook an XHR submit from there, as a possibly-cleaner approach (so regardless of how the form submit is triggered, the event is caught.)

Comment by Scott Schiller — January 26, 2006

salam…

Comment by test — January 26, 2006

I think the best way to handle a form submission via XHR is adding an event listener hooking it at the submit event. Then, inside the event listener, you can simple use the preventDefault method to tell the browser it must not execute the default action. (obviously i refer to dom level 2 event model).

Comment by Riccardo Lora — January 26, 2006

사람� 구하시거나 ��리를 찾으신다면 꼭한번 방문해보세요

기업� 채용정보 ,구�/구� 정보가 한�모�곳!!
누구나 무료로 등�하고 열람하실수 잇습니다.

비�리 사�트�며 모든 서비스는 무료로 �뤄집니다.
비싼수수료내고
구�광고 내지마세요 ^^

http://5myjob.com
http://5myjob.com

*본게시글� �리성 목�� 광고글� 아닙니다*

정보통신부�
” 정보통신ë§?ì?´ìš©ì´‰ì§„ë°?정보보호등ì—?관한법률 (법 ì œ50ì¡°ì?˜7)” ì—? ì?˜ê±°í•œ
합법� 게시글임� �혀드리며 주소� 출처는 웹검색�통해얻� 주소�며
다른어떤 ì •ë³´ë?„ 가지고있지않습니다 “수신거부” 를 ì›?하시면
아래�멜주소로 게시�� 주소를 보내주시기바�니다.

(수신거부:poi000i2@hanmail.net )
삭제비밀번호: 1234

Comment by 오마ì?´ìž¡ — February 12, 2006

쇼핑몰�나 오픈마켓 오프�� 장사하시는 사장님들
재고로 쌓�물건�나 �핑�품 �거래할수있는 사�트가
오픈했습니다

수수료나 회�가입비등 전부 무료�며 비�리로 운��니다
꼭한번 들려서 장사하시는� 많� �움��시면 좋겟습니다^^;;

http://5mymall.com
http://5mymall.com

———————————————————————-

*본게시글� �리성 목�� 광고글� 아닙니다*

정보통신부�
” 정보통신ë§?ì?´ìš©ì´‰ì§„ë°?정보보호등ì—?관한법률 (법 ì œ50ì¡°ì?˜7)” ì—? ì?˜ê±°í•œ
합법� 게시글임� �혀드리며 주소� 출처는 웹검색�통해얻� 주소�며
다른어떤 ì •ë³´ë?„ 가지고있지않습니다 “수신거부” 를 ì›?하시면
아래�멜주소로 게시�� 주소를 보내주시기바�니다.

(수신거부:poi000i2@hanmail.net )
삭제비밀번호: 1234

Comment by 오마ì?´ëª° — February 16, 2006

최고의 순위배너교환 사이트가 오픈햇습니다(비영리 사이트)

무료로 사이트 등록하시고 방문자를 3~5배정도 가져가세요

등록만으로 방문자를 보장받으 실수 잇습니다

http://5myrank.com
http://5myrank.com

>> 지금 빨리 신청하세요 빨리신청하는만큼 순위는 올라갑니다

Comment by 오마이랭크 — June 6, 2006

Here is a better link explaining whywhy XMLHttpRequest.status = 0, and XMLHttpRequest.statusText occur:
http://www.pearweb.com/javascript/XMLHttpRequest.html

Comment by Colin Pear — October 26, 2006

Thank you. I was banging my head against the wall until I realized I had my button type set to Submit. Thanks for the post…I would have never found the error on my own.

Comment by Tim — April 14, 2007

using “return false” worked for me. Thanks for posting this, Scott.

Comment by eGandalf — April 25, 2007

Non http request for local files returns status code 0 whereas http request for a file on a server returns status code 200. You should not mistake status code 0 as error for a local file. For local files, you should check for return status code 0 rather than 200.

Source: https://developer.mozilla.org/En/Using_XMLHttpRequest

Comment by remya — January 16, 2009

Even I had same problem and it was silly mistake which we do not focus on
the code was working fine in IE but had problems in Chrome and Firefox

Initilly we used Type=”submit” instead of type=”button” though the we did not have any functionality problems like updating the tables but we were getting HTTP: error 0 in the alert box when I alerted req.responseText
Using the below code solved my problem
input type=”button” name=”btnEdit5″ id=”btnEdit5″ value=”Confirm” onClick=”show_confirm()”

Comment by DileepMama — June 9, 2010

You can also get ((readyState==4) && (status==0)) if you do http to a URL that only accepts https

Comment by dlchambers — September 29, 2010

IN my case the reason I was getting status = 0 was because I was requesting a page from a different domain and Firefox security restrictions kicked in. It would have saved me about an hour if Firefox would have been kind enough to inform me that what I was doing was not permitted.

Comment by tomandlis — May 19, 2011

Leave a comment

You must be logged in to post a comment.