自动请求
默认分类 2018/06/12 17:26
hui = {};
hui.xhr = function(opt) {
opt = opt ? opt : {}
var body = opt.body || JSON.stringify(opt.data) || ''
var ctype = opt.contentType
var xhr = new XMLHttpRequest() // XMLHttpRequest 对象
var method = opt.type || (body ? 'POST' : 'GET')
xhr.open(method, opt.url, true) // POST方式,url为请求地址,true该参数表示是否异步处理
if (ctype === false || (body && window.FormData && ctype === undefined && body instanceof FormData)) {
/* no need ContentType */
} else {
xhr.setRequestHeader('Content-Type', ctype || 'application/json')
}
xhr.onreadystatechange = function(xhr, opt) {
if (xhr.readyState == 4 && xhr.status == 200) {
if (opt.success && typeof opt.success == 'function') {
window.setTimeout(function(xhr, opt) {
opt.success(!opt.responseType || opt.responseType == 'json' ?
Function('return ' + xhr.responseText)() : xhr.responseText)
}.bind({}, xhr, opt), opt.delay || 1)
}
} else if (xhr.readyState == 4 && xhr.status != 200) {
if (opt.error) opt.error(xhr.status)
}
}.bind({}, xhr, opt)
xhr.send(body) // 开始上传,发送form数据
return xhr
}
function spider1() {
if (!window.stop1 && list.length) {
var s1 = list.pop();
hui.xhr({
url: 'https://segmentfault.com' + s1 + '/info',
responseType: 'text/html',
success: function(res) {
if (res.indexOf('人类身份验证') == -1) spider1()
}
})
}
}
var list = []
>>
留言评论