不同浏览器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
- 不同浏览器XMLHttpRequest对象的status值
- 不同浏览器创建 XMLHttpRequest 对象的方法
- AJAX不同浏览器的XMLHttpRequest对象生成
- XMLHttpRequest对象的status属性
- 在不同的浏览器中创建XMLHttpRequest对象
- 创建针对不同浏览器的XMLHttpRequest对象,异步校验
- JS不同浏览器创建xmlhttprequest对象的方法
- xmlHttpRequest对象的status属性的取值
- xmlHttpRequest对象的status各种含义
- XMLHttpRequest对象的readyState和status区别
- XMLHttpRequest对象的status属性状态吗
- 创建ajax对象及解决不同浏览器对XMLHttpRequest对象的兼容问题
- 如何在HTML页面中创建兼容不同浏览器的XMLHttpRequest对象
- xmlHttpRequest的status的值的含义
- xmlHttpRequest的status的值的含义
- 兼容不同版本浏览器及版本号来创建XMLHttpRequest对象
- XMLHttpRequest的status
- 对象XMLHttpRequest属性status的含义,及 省市级联
- 如何在Excel中输入0开头数字?
- 谷歌中国年检结果合格 外媒称已低头
- centos 安装mysql遇到configure: error: No curses/termcap library found 解决方法
- 雅虎第二季度净利润2.13亿美元同比增51%
- 254440300
- 不同浏览器XMLHttpRequest对象的status值
- AWR使用学习
- 戴尔推免费浏览器安全工具 可隔离恶意软件
- 女朋友最盼男朋友做的23件事
- 关于ResolveClientUrl和ResolveUrl的使用 问题
- 一个好玩的游戏和一套星际小说下载地址
- xshell打开GUI程序失败
- zoj 1715 When Can We Meet?(水~)
- 学习