简单的原生ajax封装

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <script type="text/javascript">
            function parents(p){
                //初始化参数
                this.extendOrdther = new childs()
                this.name = p.name;
                this.age = p.age;
                this.height = p.height;
                this.weight = p.weight;
            };
            parents.prototype = {
                say:function(){
                    return '我当前的年龄为:'+ this.age
                },
                learn:function(){
                    return this.extendOrdther.play()
                }
            }
            
            
            
            function childs(){}
            childs.prototype.play = function(){
                return '我会玩lol'
            }                        
            var parentsL = new parents({
                name:'xiebin',
                age:15,
                height:150,
                weight:120
            })
             console.log(parentsL.learn())                         
/*
 *  尝试封装ajax------------------------------------------------------------------
*/
            function $(){
                //$的基本属性                
            }
            
            $.prototype = {
                ajax:function(obj){
                    //其实这里需要对传入的参数进行标准判断的!
                    this.type = obj.type;
                    this.url = obj.url;
                    this.data = obj.data;
                    this.success = obj.success;
                    this.toSend()
                },
                toSend:function(){
                    //做兼容性判断
                    var xhr = null;
                    var _this = this;
                    if(window.XMLHttpRequest){
                        xhr = new XMLHttpRequest();
                    }else if(window.ActiveXObject){
                        xhr = new ActiveXObject('Microsoft.XMLHTTP');
                    }
                    if(xhr){
                        
                        if(this.type.toLowerCase()==='post'){
                            xhr.open('POST',this.url);
                            xhr.send(this.data);
                        }else{
                            xhr.open('GET',this.url);
                            xhr.send(null);
                        }
                        xhr.onreadystatechange = function () {
                            if (xhr.readyState == 4 && xhr.status == 200) {
                                _this.success(xhr.responseText);
                                
                            }
                            else {
                                return xhr.statusText;
                            }
                        }
                    }
                }
            }
            
            
            var $ = new $();
            $.ajax({
                type:'post',
                url:'https://api.wisechain.io/trade/myAccount.do',
                data:{
                    "userId": 1123,
                    "token":123123                    
                },
                success:function(data){
                        console.log(data)
                    }
            })
        </script>
    </body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_41421227/article/details/83714814