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));
相关推荐
朝朝又沐沐5 分钟前
算法竞赛阶段二-数据结构(36)数据结构双向链表模拟实现
开发语言·数据结构·c++·算法·链表
YGY Webgis糕手之路6 分钟前
OpenLayers 综合案例-轨迹回放
前端·经验分享·笔记·vue·web
魔尔助理顾问21 分钟前
系统整理Python的循环语句和常用方法
开发语言·后端·python
90后的晨仔24 分钟前
🚨XSS 攻击全解:什么是跨站脚本攻击?前端如何防御?
前端·vue.js
Ares-Wang25 分钟前
JavaScript》》JS》 Var、Let、Const 大总结
开发语言·前端·javascript
90后的晨仔29 分钟前
Vue 模板语法完全指南:从插值表达式到动态指令,彻底搞懂 Vue 模板语言
前端·vue.js
德育处主任1 小时前
p5.js 正方形square的基础用法
前端·数据可视化·canvas
烛阴1 小时前
Mix - Bilinear Interpolation
前端·webgl
90后的晨仔1 小时前
Vue 3 应用实例详解:从 createApp 到 mount,你真正掌握了吗?
前端·vue.js
德育处主任1 小时前
p5.js 矩形rect绘制教程
前端·数据可视化·canvas