不同浏览器XMLHttpRequest对象的status值

来源:互联网 发布:ubuntu 14 trusty 编辑:程序博客网 时间:2024/04/29 02:34

http://heaven.branda.to/~thinker/GinGin_CGI.py/show_id_doc/211

 

下面文章是摘自 User-Proofing Ajax 這篇文章的討論和留言。本文的部分,值得 AJAX 的 programmer 一看,後續的討論更是點出了許多問題。

而摘錄的這個留言,其實才是我一開始想透過 google 找的資料。因為我在讀取 XMLHttpRequest.status 時,發覺 IE 傳回 1223 這種奇怪的 status code 。試了很久,確定不是程式的錯後,我才用 google 找看看別人怎麼說,結果找到這段討論/留言。下面整理一些常見的錯誤狀況,和 opera/FF/IE 三種瀏灠器反應,並進行對照。對 AJAX 的 programmer ,應該是很有用的資料。

 

摘錄/備份

Its good to see someone giving some effort to error handling in ajax. I tried searching for quite a while before resorting to my own experiments. I thought some people out there might find this useful.

Any number of problems can arise which may result in communications problems between your ajax application and the server and unfortunately the different XMLHttpRequest objects do not handle them in a standard way.

The server may be offline or your route to the server may be down resulting in not being able to establish a connection, a 30 second timeout is not the best solution here.

opera 9 will call onreadystatechange with req.readyState=4 and req.status=0 firefox 1 will call onreadystatechange with req.readyState=4 and req.status exception ie 6 will call onreadystatechange with req.readyState=4 and req.status=12029

Those are easily enough detected, use try and catch to handle the exception. Your connection to the server could unexpectedly close due to network problems or a server crash. These are much harder to deal with.

opera 9 will call onreadystatechange with req.readyState=4 and req.status=0 ie 6 will call onreadystatechange with req.readyState=4 and req.status=12152 firefox 1 will call onreadystatechange with req.readyState=4 and req.status=200

The firefox response is a big problem here, 200 is the HTTP status code sent by the server to indicate success but firefox will give a status of 200 if the server disconnects without sending any status code at all which means it is not reliable to use a 200 status code as an indication of success. If we were posting a message on a bbs when the error occured then firefow would indicate success but our message would not be posted.

So how should we detect a successful ajax request? a fully valid response document would be one indication but not all ajax requests return a document, XML, JSON, plain text or anything. We could use another status code but there are problems there too. The code which should be returned if there is no document body is 204 “No Content”, unfortunately if we return 204 then opera9 will not call onreadystatechange so our ajax application does not see a response at all. IE6 will call onreadystatechange with req.readyState=4 and req.status=1223 instead. Fortunately 202 “Accepted” does work reliably.

There are similar problems with HTTP status codes in the 500 range (Failure codes).

Believe it or not it is possible to code your ajax application to work reliably despite all these problems but it helps if you know what to expect.

posted at 04:06 pm on December 08, 2006 by Tony Gallagher