zoukankan      html  css  js  c++  java
  • 浏览器自定义Ajax函数批量调接口

    function Ajax(type, url, data, success, failed){
        // 创建ajax对象
        var xhr = null;
        if(window.XMLHttpRequest){
            xhr = new XMLHttpRequest();
        } else {
            xhr = new ActiveXObject('Microsoft.XMLHTTP')
        }
     
        var type = type.toUpperCase();
        // 用于清除缓存
        var random = Math.random();
     
        if(type == 'GET'){
            if(data){
                xhr.open('GET', url + '?' + data, true);
            } else {
                xhr.open('GET', url + '?t=' + random, true);
            }
            xhr.send();
        } else if(type == 'POST'){
            xhr.open('POST', url, true);
            // 如果需要像 html 表单那样 POST 数据,请使用 setRequestHeader() 来添加 http 头。
            xhr.setRequestHeader("Content-type", "application/json");
            xhr.send(JSON.stringify(data));
    	//xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            //xhr.send(data);
        }
    	
    	// 处理返回数据
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4){
                if(xhr.status == 200){
    		console.log("xhr success");
                    success(xhr.responseText);
                } else {
                    if(failed){
    		      console.log("xhr failed");
                          failed(xhr.status);
                    }
                }
            }
        }
    }
    
    var e = function(error){
        console.log(error);
    }
    
    var f = function(data){
       console.log(data);
    }
    
    
    document.getElementsByClassName("ant-pagination-item-link")[1].click();
    for (var i = 0; i < document.getElementsByClassName("ant-list-item-meta-title").length; i++) {
        var taskId = document.getElementsByClassName("ant-list-item-meta-title")[i].childNodes[0].innerText;
        console.log(taskId);
        Ajax('post', "https://cc.xxx.com/xxx/v1/finish", {
            "taskId": taskId,
            "operator": "xxx",
            "comment": "1"
        }, f, e);
    }
    
  • 相关阅读:
    异或运算的运用
    HDU1251(统计难题)
    待解决题目
    tarjan算法模板
    FZU1465
    边双联通分量(构造边双联通图)
    UVa 12558
    codeforce Error Correct System
    codeforce The Art of Dealing with ATM
    ~0u >> 1
  • 原文地址:https://www.cnblogs.com/umgsai/p/12986853.html
Copyright © 2011-2022 走看看