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

    将原生ajax进行封装

    function ajax() {
                    var ajaxData = {
                        type: arguments[0].type || "GET",
                        url: arguments[0].url || "",
                        async: arguments[0].async || "true",
                        data: arguments[0].data || null,
                        dataType: arguments[0].dataType || "text",
                        contentType: arguments[0].contentType || "application/x-www-form-urlencoded",
                        beforeSend: arguments[0].beforeSend || function() {},
                        success: arguments[0].success || function() {},
                        error: arguments[0].error || function() {}
                    }
                    ajaxData.beforeSend()
                    var xhr = createxmlHttpRequest();
                    xhr.responseType = ajaxData.dataType;
                    xhr.open(ajaxData.type, ajaxData.url, ajaxData.async);
                    xhr.setRequestHeader("Content-Type", ajaxData.contentType);
                    xhr.send(convertData(ajaxData.data));
                    xhr.onreadystatechange = function() {
                        if(xhr.readyState == 4) {
                            if(xhr.status == 200) {
                                ajaxData.success(xhr.response)
                            } else {
                                ajaxData.error()
                            }
                        }
                    }
                }
    
                function createxmlHttpRequest() {
                    if(window.ActiveXObject) {
                        return new ActiveXObject("Microsoft.XMLHTTP");
                    } else if(window.XMLHttpRequest) {
                        return new XMLHttpRequest();
                    }
                }
    
                function convertData(data) {
                    if(typeof data === 'object') {
                        var convertResult = "";
                        for(var c in data) {
                            convertResult += c + "=" + data[c] + "&";
                        }
                        convertResult = convertResult.substring(0, convertResult.length - 1)
                        return convertResult;
                    } else {
                        return data;
                    }
                }

    ajax实现,写起来和jQuery差不多

    ajax({
                    type: "POST",
                    url: "http://share.vr39.com/rest/link/importUserPanoramData",
                    dataType: "json",
                    data: {
                        "viewUuid": data
                    },
                    beforeSend: function() {
                        //some js code 
                    },
                    success: function(msg) {
                        alert(msg.message)
                    },
                    error: function() {
                        alert("error")
                    }
                });
  • 相关阅读:
    python截取视频中的某一段,保存为avi结尾的视频
    文章内容过长,将此内容转为pdf的方式(使用node)
    typescript常见问题集锦
    利用matplotlib中imshow()函数绘图
    如何查看Linux系统安装时间
    php包含那点事情[WOOYUN]
    中间人攻击利用框架bettercap测试
    java 递归方法
    java方法重载
    JVM 之栈结构
  • 原文地址:https://www.cnblogs.com/Java-Starter/p/7866222.html
Copyright © 2011-2022 走看看