var myHeaders = new Headers();
myHeaders.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
fetch('index_articlelist.html', {
    method: 'POST',
    headers: myHeaders,
    credentials: 'same-origin',
    cache: 'default',
    body: 'type=xxgg&page=1&size=20'
}).then(res=>res.json()).then(obj=>console.log(obj))

在爬学校信息网的时候用到的,主要是尝试一下最近看到的 fetch()
主要踩了 2 点:

  • jQuery 会帮我们设置 Content-Type ,而 fetch 默认就是 plain/text,在这里会导致请求失败,所以手动 append 一个请求头。
  • fetch 需要设置 credentials 项,才会自动提交 Cookies 。这里不是跨域(直接控制台运行的),所以用 same-origin 而不需要 include 。

相关文章:

  • 2021-11-09
  • 2022-01-20
猜你喜欢
  • 2021-09-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-07
  • 2022-01-05
  • 2021-04-17
相关资源
相似解决方案