⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 json.js

📁 简单博客功能实现
💻 JS
字号:
// JScript 文件

// Json对象,用于向服务器发送请求并返回,同时可以直接请求一个文件
X2.Json=Class.create();
X2.Json.prototype={
    initialize:function(url,tipBox){
        this.using=false;
        this.url=url;
        this.data=null;
        this.tipBox=tipBox;
    }
    ,open:function(oper,para,method){
        //实现para参数的重载效果,可以是string或者object
        if(typeof(para)=='object')para=$H(para).toQueryString();
        if(!oper){
            oper='none';
            para='';
        }
        if(!method)method='get';
        this.using=true;
        var self=this;
        var option={
	        parameters:para,
	        method:method,
	        onSuccess:function(transport){
				var s=transport.responseText.replace(/[\n\r]/g,'');
				try{
					self.data=eval('('+s+')');
                }catch(e){
                    self.debug(s);
                }
                this.using=false;
                self.onsuccess();
	        },
	        onFailure:function(transport){
	            self.using=false;
	            self.onfailure();
	            self.debug(transport);
		    }
	    }
	    var c=this.url.indexOf('?')>=0 ? '&':'?';
        this.request=new Ajax.Request(this.url+c+'s='+Math.random()+'&oper='+oper,option);
    }
    //传统用法,得到纯粹的返回值
    ,openForText:function(oper,para,method){
        //实现para参数的重载效果,可以是string或者object
        if(typeof(para)=='object')para=$H(para).toQueryString();
        if(!oper){
            oper='none';
            para='';
        }
        if(!method)method='get';
        this.using=true;
        var self=this;
        var option={
	        parameters:para,
	        method:method,
	        onSuccess:function(transport){
                self.onsuccess(transport.responseText);
	        },
	        onFailure:function(transport){
	            self.using=false;
	            self.onfailure();
	            self.debug(transport);
		    }
	    }
	    var c=this.url.indexOf('?')>=0 ? '&':'?';
        this.request=new Ajax.Request(this.url+c+'s='+Math.random()+'&oper='+oper,option);
    }
    ,abort:function(){
        this.request.transport.abort();
    }
    ,getData:function(){
        return this.data;
    }
    ,loadFile:function(url){
		var self=this;
		var option={
	        onSuccess:function(transport){
				self.data=transport.responseText;
				self.onsuccess();
	        }
	        ,onFailure:function(transport){
				self.using=false;
				self.onfailure();
				self.debug(transport);
		    }
	    }
	    var c=url.indexOf('?')>=0 ? '&':'?';
        var request=new Ajax.Request(url+c+'s='+Math.random(),option);
    }
    ,onsuccess:function(){
		var jso=this.data;
		if(typeof jso=='string'){
		    this.onlogicalsuccess({data:jso});
		}else if(jso.success)this.onlogicalsuccess(jso);
		else this.onlogicalfailure(jso);
    }
    ,onfailure:function(){
		try{
			this.tipBox.show('数据传输失败,请检查网络或者重试。');
		}catch(e){}
    }
    ,onlogicalsuccess:function(){}
    ,onlogicalfailure:function(jso){
        try{
			this.tipBox.show(jso.error);
	    }catch(e){};
    }
    ,debug:function(transport){
        //新开窗口显式错误信息
		//var doc=window.open('');
		//doc.document.write(transport.responseText);
    }
    ,debug2:function(transport){
        //alert(transport.responseText);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -