![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/None.gif)
HTTP状态码
在列表6中,XMLHttpRequest的status属性被测试用来确定请求是否成功完成。当处理简单的GET与POST请求,你可以认为只要不是200(OK)的状态就表示发生了错误。若服务器发送了一个重定向响应(例如,301或302),浏览器会透明地完成重定向并从新位置获取相应的资源;XMLHttpRequest不会看到重定向状态码。同时,浏览器自动添加一个缓存控制:对于所有XMLHttpRequest都使用no-cache header,这样客户端代码就可以不用处理304(not-modified)响应。
关于getReadyStateHandler()
getReadyStateHandler()是相对比较复杂的一段代码,特别当你不能熟悉阅读JavaScript时。折中方案是在你的JavaScript库中包含此函数,你可以简单地处理Ajax服务器响应,而不用去注意XMLHttpRequest的内部细节。重要是你自己要理解在代码中如何使用getReadyStateHandler()。
在列表3中,你看到getReadyStateHandler()被这样调用:
handlerFunction=getReadyStateHandler(req,updateCart)。
由它返回的函数将会检查在req变量中的XMLHttpRequest是否已完成,并调用由updateCart指定的回调方法处理响应XML。
提取购物车数据
列表7中展示了updateCart()中的代码。此函数使用DOM来解析购物车XML文档,并更新WEB页面(参见列表1)来反映新的购物车内容。注意对用来提取数据的XML DOM的调用。Cart元素上生成的属性,即序列化时生成的时间戳,通过检测它可以保证不会用老的数据来覆盖新的购物车数据。Ajax请求天生就是异步的,通过这个检测可以有效避免在过程外到达的服务器响应的干扰。
列表7:更新页面来反映出购物车XML文档内容
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b854634c0904529d4018c4c3336be836.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://wdtclv.cnblogs.com/Images/OutliningIndicators/None.gif)
到现在,关于Ajax处理过程的教程已经结束,也许你想让应用运行起来,并看看它的实际运作(参见下载部分)。这个例子非常简单,有非常大的改进的余地。比如,我在服务器端代码中包含了从购物车中移除条目的代码,但从客户端UI中没有访问的途径。作为一个练习,尝试在现有的JavaScript基础上实际这个功能。
。 使用Ajax的挑战