[转帖]Mootools源码分析-35 -- Request.XML&Request.JSON

原帖地址:http://space.flash8.net/space/?uid-18713-action-viewspace-itemid-407569

原作者:我佛山人

 

代码
// 响应输出为JSON的Ajax请求类
Request.JSON  =   new  Class({
    
// 继承自Request
    Extends: Request,
    options: {
        
// 是否强制返回的JSON为严格格式
        secure:  true
    },

    
// 覆盖父类构造函数
    initialize:  function (options)    {
        
// 调用父类同名方法
        arguments.callee.parent(options);
        
// 设置HTTP头部信息
         this .headers.extend({ ' Accept ' ' application/json ' ' X-Request ' ' JSON ' });
    },

    
// 请求成功,覆盖Request类的同名方法
    success:  function (text)    {
        
// 添加解释完的JSON对象到响应对象
         this .response.json  =  JSON.decode(text,  this .options.secure);
        
// 调用父类方法,触发成功事件
         this .onSuccess( this .response.json, text);
    }
});

/*
自己扩展的,用于响应输出为XML的Ajax请求类
主要是为返回的xml对象附加支持XPath的selectNodes和selectSingleNode方法
因为IE本身已经支持这两方法,所以主要是针对支持XPath的其它浏览器
document.evaluate方法的用法可参考以下网址:
http://developer.mozilla.org/en/docs/DOM:document.evaluate
http://wiki.mozcn.org/index.php/Firefox:Dive_Into_Greasemonkey/4.6._Doing_something_for_every_element_with_a_certain_attribute
*/
Request.XML 
=   new  Class({

    
// 继承自Request
    Extends: Request,

    
// 请求成功,覆盖Request类的同名方法
    success:  function (text, xml)    {
        
// 因为IE本身已经支持这两方法,所以主要是针对支持XPath的其它浏览器
         if (Browser.Features.xpath)    {
            
// 扩展实现selectNodes方法
            xml.selectNodes  =   function (xpath)    {
                
var  nodes  =  [], node;
                
// 几个参数的意义
                 var  result  =   this .evaluate(xpath,  this this .createNSResolver( this .documentElement), XPathResult.ORDERED_NODE_ITERATOR_TYPE,  null ) ;
                
if  (result)     while (node  =  result.iterateNext()) nodes.push(node);
                
return  nodes;
            }; 
            
// 扩展实现selectSingleNode方法
            xml.selectSingleNode  =   function (xpath)    {
                
var  result  =   this .evaluate(xpath,  this this .createNSResolver( this .documentElement),  9 null );
                
return  (result  &&  result.singleNodeValue)  ?  result.singleNodeValue : [];
            };
        }
        
// 调用父类方法,触发成功事件
         this .onSuccess(xml, text);
    }
});

 

转载于:https://www.cnblogs.com/maapaa/articles/mootools-s-35.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值