浏览器自定义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); }
相关推荐
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03
TONIYH 2020-07-22
时光如瑾雨微凉 2020-07-19
83510998 2020-07-18
坚持着执着 2020-07-16
jiaguoquan00 2020-07-07
李永毅 2020-07-05
坚持着执着 2020-07-05