zoukankan      html  css  js  c++  java
  • 封装原生Ajax

    
    
        var Chef = {
            createAjax:function() {
                var xhr = null;
                try {
                    //IE系列浏览器
                    xhr = new ActiveXObject("microsoft.xmlhttp");
                } catch (e1) {
                    try {
                        //非IE浏览器
                        xhr = new XMLHttpRequest();
    
                    } catch (e2) {
                        window.alert("您的浏览器不支持ajax,请更换!");
                    }
                }
                return xhr;
            },
            ajax:function(conf) {
                // 初始化 //type参数,可选
                var type = conf.type,
                //url参数,必填
                    url = conf.url,
                //data参数可选,只有在post请求时需要
                    data = conf.data,
                //datatype参数可选
                    dataType = conf.dataType,
                //回调函数可选
                    success = conf.success;
                if (type == null){
                    //type参数可选,默认为get
                    type = "get";
                } if (dataType == null){
                    //dataType参数可选,默认为text
                    dataType = "text";
                } // 创建ajax引擎对象
                var xhr = this.createAjax();
                // 打开
                xhr.open(type, url, true);
                // 发送
                if (type == "GET" || type == "get") {
                    xhr.send(null);
                } else if (type == "POST" || type == "post") {
                    xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
                    xhr.send(data);
                }
                xhr.onreadystatechange = function() {
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        if(dataType == "text"||dataType=="TEXT") {
                            if (success != null){
                                //普通文本
                                success(xhr.responseText);
                            }
                        }else if(dataType=="xml"||dataType=="XML") {
                            if (success != null){ //接收xml文档
                                success(xhr.responseXML);
                            }
                        }else if(dataType=="json"||dataType=="JSON") {
                            if (success != null){
                                //将json字符串转换为js对象
                                success(eval("("+xhr.responseText+")"));
                            }
                        }
                    }
                };
            }
        }

    使用方法:

        Chef.ajax({
            type:"post",
            url:"../data/data.json",
            data:"name=dipoo&info=good",
            dataType:"json",
            success:function(data){
                alert(data.result);
            }
        });
  • 相关阅读:
    iOS开源App整理
    iOS9 3DTouch 之 Home Screen Quick Actions
    UITabbarController & UITabbar 学习
    Linux一些最基础操作
    logo的表现形式
    LOGO设计中出现文字背后的意义
    标志设计中选择合适的字体
    sketch制作LOGO(三) ---大熊猫
    sketch制作LOGO(二) ---樱花婆婆
    十多个app引导页面欣赏
  • 原文地址:https://www.cnblogs.com/chefweb/p/6039372.html
Copyright © 2011-2022 走看看