Fetch数据请求

什么是Polyfill?

Polyfill是一个js库,主要抚平不同浏览器之间对js实现的差异。比如,html5storage(session,local), 不同浏览器,不同版本,有些支持,有些不支持。PolyfillPolyfill有很多,在GitHubhttps://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills),帮你把这些差异化抹平,不支持的变得支持了(典型做法是在IE浏览器中增加 window.XMLHttpRequest ,内部实现使用 ActiveXObject。)

提到Polyfill,不得不提shim。 ``polyfillshim的一种。
shim是将不同 api封装成一种,比如 jQuery$.ajax 封装了 XMLHttpRequestIEActiveXObject方式创建xhr对象。它将一个新的API引入到一个旧的环境中,而且仅靠旧环境中已有的手段实现。

XMLHttpRequest是一个设计粗糙的API,配置和调用方式非常混乱,而且基于事件的异步模型,写起来不友好,容易掉入回调地狱中。

W3C新标准出来后,给了我们一个新的通道Fetch,Fetch取代了XMLHttpRequest,它是集成在浏览器标准里面的,所以它就是嫡系,它虽然是嫡系,但是它的兼容性不是很好,也没有第三方库:axios好用,所以用它比较少

(局部页面刷新技术没有改变,只是说一前做异步的时候用XMLHttpRequest来实现,现在我们可以用Fetch来实现,Fetch是基于polyfill的)

Fetch是浏览器自带的,无需再次引入其他的插件包了。

javascript 复制代码
//Get请求
fetch("http://localhost:8080/users")
    .then(res => res.json()) //这里和获取到的不是返回值,只是设定返回的数据类型,如果服务端返回的是Json那么这里就res.json(),如果服务端返回的是文本,那么这里就res.text()
    .then(res => {
        console.log(res); //这里才是返回具体数据
    })

//Post请求
fetch("http://localhost:8080/users", { method: 'post', headers: { "content-type": "application/json", body: JSON.stringify({ username: "张三", age: 18 }) } })
    .then(res => res.json()) //这里和获取到的不是返回值,只是设定返回的数据类型,如果服务端返回的是Json那么这里就res.json(),如果服务端返回的是文本,那么这里就res.text()
    .then(res => {
        console.log(res); //这里才是返回具体数据
    })

//Post请求
fetch("http://localhost:8080/users", { method: 'post', headers: { "content-type": "application/x-www-formurlencoded", body: "name=王五&age=20" } })
    .then(res => res.json()) //这里和获取到的不是返回值,只是设定返回的数据类型,如果服务端返回的是Json那么这里就res.json(),如果服务端返回的是文本,那么这里就res.text()
    .then(res => {
        console.log(res); //这里才是返回具体数据
    })

//Put请求
fetch("http://localhost:8080/users", { method: 'put', headers: { "content-type": "application/json", body: JSON.stringify({ username: "张三", age: 18 }) } })
    .then(res => res.json()) //这里和获取到的不是返回值,只是设定返回的数据类型,如果服务端返回的是Json那么这里就res.json(),如果服务端返回的是文本,那么这里就res.text()
    .then(res => {
        console.log(res); //这里才是返回具体数据
    })

//Delete请求
fetch("http://localhost:8080/users/5", { method: 'post', headers: { "content-type": "application/json", body: JSON.stringify({ username: "张三", age: 19 }) } })
    .then(res => res.json()) //这里和获取到的不是返回值,只是设定返回的数据类型,如果服务端返回的是Json那么这里就res.json(),如果服务端返回的是文本,那么这里就res.text()
    .then(res => {
        console.log(res); //这里才是返回具体数据
    })

axios:因为axios是第三方库,所以需要引入包

javascript 复制代码
//使用axios第三方库需要引入插件
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

axios.get("http://localhost:8080/users")
.then(res => console.log(res.data))
.catch(err => console.log(err));

axios.get("http://localhost:8080/users", { params: { name: "张三" } })
.then(res => console.log(res.data))
.catch(err => console.log(err));

// post-json
axios.post("http://localhost:8080/users", { name: "张三", age: 21 })
.then(res => console.log(res.data))
.catch(err => console.log(err));

// post-form
axios.post("http://localhost:8080/users", "name=张三&age=21")
.then(res => console.log(res.data))
.catch(err => console.log(err));


axios.post("http://localhost:8080/users/5", { name: "张三", age: 21 })
.then(res => console.log(res.data))
.catch(err => console.log(err));

axios.delete("http://localhost:8080/users/5")
.then(res => console.log(res.data))
.catch(err => console.log(err));
相关推荐
IT_陈寒21 小时前
Redis 高并发实战:我从 5000QPS 优化到 5W+ 的7个核心策略
前端·人工智能·后端
m0_7381207221 小时前
内网横向靶场——记录一次横向渗透(三)
开发语言·网络·安全·web安全·网络安全·php
songroom21 小时前
Rust: 量化策略回测与简易线程池构建、子线程执行观测
开发语言·后端·rust
jz_ddk21 小时前
[数学基础] 瑞利分布:数学原理、物理意义及Python实验
开发语言·python·数学·概率论·信号分析
大G的笔记本21 小时前
Java JVM 篇常见面试题
java·开发语言·jvm
vortex521 小时前
ASP vs ASP.NET vs ASP.NET Core:三代微软 Web 技术核心区别解析
前端·microsoft·asp.net
ZHE|张恒21 小时前
深入理解 Java 双亲委派机制:JVM 类加载体系全解析
java·开发语言·jvm
Apifox1 天前
如何在 Apifox 中使用「模块」合理地组织接口
前端·后端·测试
冰暮流星1 天前
css之flex属性
前端·css