Thursday, January 26th, 2006
XMLHttpRequest and status code: 0
<>p>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.
Related Content:











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.)salam…
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).
사람ì?„ 구하시거나 ì?¼ìž?리를 ì°¾ìœ¼ì‹ ë‹¤ë©´ ê¼í•œë²ˆ 방문해보세요
기업ì?˜ ì±„ìš©ì •ë³´ ,구ì?¸/구ì§? ì •ë³´ê°€ 한ë?°ëª¨ì?¸ê³³!!
누구나 무료로 등ë¡?í•˜ê³ ì—´ëžŒí•˜ì‹¤ìˆ˜ 잇습니다.
비ì˜?리 사ì?´íЏì?´ë©° ëª¨ë“ ì„œë¹„ìŠ¤ëŠ” 무료로 ì?´ë¤„집니다.
비싼수수료내ê³
구ì?¸ê´‘ê³ ë‚´ì§€ë§ˆì„¸ìš” ^^
http://5myjob.com
http://5myjob.com
*본게시글ì?€ ì˜?리성 목ì ?ì?˜ ê´‘ê³ ê¸€ì?´ 아닙니다*
ì •ë³´í†µì‹ ë¶€ì?˜
” ì •ë³´í†µì‹ ë§?ì?´ìš©ì´‰ì§„ë°?ì •ë³´ë³´í˜¸ë“±ì—?ê´€í•œë²•ë¥ (법 ì œ50ì¡°ì?˜7)” ì—? ì?˜ê±°í•œ
합법ì ? 게시글임ì?„ ë°?혀드리며 주소ì?˜ 출처는 웹검색ì?„통해얻ì?€ 주소ì?´ë©°
다른어떤 ì •ë³´ë?„ ê°€ì§€ê³ ìžˆì§€ì•ŠìŠµë‹ˆë‹¤ “ìˆ˜ì‹ ê±°ë¶€” 를 ì›?하시면
아래�멜주소로 게시�� 주소를 보내주시기바�니다.
(ìˆ˜ì‹ ê±°ë¶€:poi000i2@hanmail.net )
ì‚ì œë¹„ë°€ë²ˆí˜¸: 1234
쇼핑몰�나 오픈마켓 오프�� 장사하시는 사장님들
ìž¬ê³ ë¡œ 쌓ì?¸ë¬¼ê±´ì?´ë‚˜ ë?¤í•‘ìƒ?í’ˆ ì§?ê±°ëž˜í• ìˆ˜ìžˆëŠ” 사ì?´íŠ¸ê°€
오픈했습니다
수수료나 회ì›?가입비등 ì „ë¶€ 무료ì?´ë©° 비ì˜?리로 ìš´ì˜?ë?©ë‹ˆë‹¤
ê¼í•œë²ˆ ë“¤ë ¤ì„œ 장사하시는ë?° ë§Žì?€ ë?„움ì?´ë?˜ì‹œë©´ 좋겟습니다^^;;
http://5mymall.com
http://5mymall.com
———————————————————————-
*본게시글ì?€ ì˜?리성 목ì ?ì?˜ ê´‘ê³ ê¸€ì?´ 아닙니다*
ì •ë³´í†µì‹ ë¶€ì?˜
” ì •ë³´í†µì‹ ë§?ì?´ìš©ì´‰ì§„ë°?ì •ë³´ë³´í˜¸ë“±ì—?ê´€í•œë²•ë¥ (법 ì œ50ì¡°ì?˜7)” ì—? ì?˜ê±°í•œ
합법ì ? 게시글임ì?„ ë°?혀드리며 주소ì?˜ 출처는 웹검색ì?„통해얻ì?€ 주소ì?´ë©°
다른어떤 ì •ë³´ë?„ ê°€ì§€ê³ ìžˆì§€ì•ŠìŠµë‹ˆë‹¤ “ìˆ˜ì‹ ê±°ë¶€” 를 ì›?하시면
아래�멜주소로 게시�� 주소를 보내주시기바�니다.
(ìˆ˜ì‹ ê±°ë¶€:poi000i2@hanmail.net )
ì‚ì œë¹„ë°€ë²ˆí˜¸: 1234
ìµœê³ ì˜ ìˆœìœ„ë°°ë„ˆêµí™˜ 사ì´íŠ¸ê°€ 오픈햇습니다(비ì˜ë¦¬ 사ì´íЏ)
무료로 사ì´íЏ 등ë¡í•˜ì‹œê³ 방문ìžë¥¼ 3~5ë°°ì •ë„ ê°€ì ¸ê°€ì„¸ìš”
등ë¡ë§Œìœ¼ë¡œ 방문ìžë¥¼ 보장받으 실수 잇습니다
http://5myrank.com
http://5myrank.com
>> 지금 빨리 ì‹ ì²í•˜ì„¸ìš” ë¹¨ë¦¬ì‹ ì²í•˜ëŠ”ë§Œí¼ ìˆœìœ„ëŠ” 올ë¼ê°‘니다
Here is a better link explaining whywhy XMLHttpRequest.status = 0, and XMLHttpRequest.statusText occur:
http://www.pearweb.com/javascript/XMLHttpRequest.html
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.
using “return false” worked for me. Thanks for posting this, Scott.
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
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()”
You can also get ((readyState==4) && (status==0)) if you do http to a URL that only accepts https
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.