微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

浏览器自定义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);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐