首页前端开发正文

如何用JavaScript发http get请求?

朱绪2021-12-09472JavaScript

怎么样用js代码发get请求?

不建议使用XMLHttpRequest,我们不妨用fetch。

fetch与xhr相比有很多优势,比如:可以用在service worker中...

来个fetch的示例:

fetch("url")
    .then(res => res.json())
    .then(data => console.log("有数据了:", data))