导航栏: 首页 评论列表

request 函数

默认分类 2020/11/16 04:07

代码如下:

function createXHR(url) {
  let xhr = new XMLHttpRequest()
  xhr.open('GET', url, true)
  xhr.send()
}

代码如下:

function createLink(src) {
  var style = document.createElement('link');style.rel = 'stylesheet';style.type = 'text/css';
  style.href = src;
  document.getElementsByTagName('HEAD').item(0).appendChild(style);
}
var list = document.querySelectorAll('.wordbook-wordlist-name')
list.forEach(it=>{ for(let i = 0; ++i < 12; ) { createLink(it.children[0].href + '?page=' + i); } })

HEAD请求:

function createXHR(url) {
  let xhr = new XMLHttpRequest()
  xhr.open('HEAD', url, true)
  xhr.onreadystatechange = () => {
    if (xhr.readyState !== 4) return ''
    const status = xhr.status;
    if (status == 404) {
      console.log('File not exist');
    }
  }
  xhr.send()
}

Fetch请求:

function doit () {
  fetch('/403')
  // 返回403在这里判断
  .then(function(res) {
    if (res.status === 403) {
      console.log('HTTP Status 403')
    }
    // res.text() 和 res.json() 均会返回一个Promise对象
    res.text()
  })
  .then(function(data) {
    console.log(data)
  })
  // 网络中断时触发
  .catch(function(err) {
    console.log('Fetch Error : %S', err)
  })
}


>> 留言评论