<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>jsonp</title>
</head>
<body>
<script type="text/javascript" src="jquery-1.6.2.js"></script>
<!-- <script type="text/javascript" src="jquery-1.11.1.js"></script> -->
<script type="text/javascript">
(function(root){
    if(typeof(Ajax)=="undefined"&&!Ajax) var Ajax={};
    Ajax.request=function(obj){
        var xhr=new Ajax.creat();
        return xhr.request(obj);
    }
    Ajax.creat=function(){
        this.ajax=this.get();
    }
    Ajax.creat.prototype={
        get:function(){
            try{return new XMLHttpRequest();}catch(e){}
            try{return new ActiveXObject('Msxml2.XMLHTTP.6.0');}catch(e){}
            try{return new ActiveXObject('Msxml2.XMLHTTP.4.0');}catch(e){}
            try{return new ActiveXObject('Msxml2.XMLHTTP.3.0');}catch(e){}
            try{return new ActiveXObject('Msxml2.XMLHTTP');}catch(e){}
            try{return new ActiveXObject('MSXML3.XMLHTTP');}catch(e){}
            try{return new ActiveXObject('MSXML.XMLHTTP');}catch(e){}
            try{return new ActiveXObject('Microsoft.XMLHTTP');}catch(e){}
            try{return new ActiveXObject('MSXML2.ServerXMLHTTP');}catch(e){}
            return null;
        },
        request:function(obj){
            var self=this,ajax=self.ajax;
            if(typeof(obj)!=="object"||ajax==null) return;
            var url=obj.url,
                type=(obj.type&&obj.type.toUpperCase())||"GET",
                data=obj.data||null,
                async=typeof(obj.async)=="boolean"? obj.async : true,
                success=((typeof(obj.success)=="function")&&obj.success)||new Function(),
                error=((typeof(obj.error)=="function")&&obj.error)||new Function();

            if(typeof(data)=="object") data=self.objectToStr(data);

            if(type=="JSONP"){
                
                var jsonp=obj.jsonp||"callback",
                     jsonpCallback=obj.jsonpCallback||("myfn"+parseInt(new Date().getTime()*Math.random())),
                     scrLoad=0,callback=success,error=error;
                    var script=document.createElement("script"),scriptId="script"+Math.random();
                    url+=(url.indexOf("?")==-1?"?":"&")+data;
                    console.log(url)
                    url+="&"+jsonp+"="+jsonpCallback;
                    script.id=scriptId;
                    script.src=url;
                    
                    root[jsonpCallback]=function(){
                        var arg=arguments;
                    //判断script是否加载完毕 IE/Opera支持readystatechange
                        if(window.navigator.userAgent.toLowerCase().indexOf("msie")>-1){
                            script.onreadystatechange=function(){
                                if(script.readyState === "loaded" || script.readyState === "complete"){
                                    alert(script.readyState)
                                    callback.apply(null,arg);
                                    scrLoad=1;
                                    _remove();
                                }
                            }
                        }else{
                            script.onload=function(){
                                if(scrLoad) return;
                                callback.apply(null,arg);
                                _remove();
                            }
                        }
                    }

                    function _remove(){
                        document.getElementsByTagName("head")[0].removeChild(script);
                    }

                    document.getElementsByTagName("head")[0].appendChild(script);
                
                return;
            }
            
            if(type=="GET"&&data) url+=(url.indexOf("?")==-1?"?":"&")+data;

            ajax.open(type,url,async);

            if(type=="POST") ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded;");

            ajax.send(data);

            ajax.onreadystatechange=function(){
                self.handle(ajax,success,error);
            }
            return ajax;
        },
        objectToStr:function(s){
            var str="";
            for(var x in s){
                str+= x+"="+s[x]+"&";
            }
            str=str.substring(0,str.lastIndexOf("&"));
            return str;
        },
        handle:function(xhr,success,error){
            if(xhr.readyState==4){
                var status=xhr.status;
                if(status>=200&&status<=300) success(xhr);
                    else error(xhr);
            }else{}
        }
    }
    root.ajax={request:Ajax.request}
})(window)
//url,type,data,async,success,error
</script>

<script type="text/javascript">
ajax.request({url:"test.php",data:"shabi=wangxuan",type:"jsonp",success:function(r){alert(r+"1");}})
ajax.request({url:"test.php",data:"shabi=wangxuan",type:"jsonp",success:function(r){alert(r+"2");}})
</script>
</body>
</html>