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));
相关推荐
huangdong_9 分钟前
淘宝商品SKU图自动分类技术深度解析:从DOM解析到智能归档
开发语言·javascript·ecmascript
梦曦i11 分钟前
uni-router v1.1.1发布:守卫超时保护+路由监听
前端·uni-app
阿正的梦工坊12 分钟前
【Rust】12-借用检查器与非词法生命周期
开发语言·后端·rust
qq_25183645730 分钟前
基于java Web网络订餐系统设计与实现 源码文档
java·开发语言·前端
秋936 分钟前
3年经验Python后端转AI Engineer:3个月实战转型计划(2026版)
开发语言·人工智能·python
凡人叶枫1 小时前
Effective C++ 条款17:以独立语句将 newed 对象置入智能指针
java·linux·开发语言·c++·算法
飞天狗1111 小时前
零基础JavaWeb入门——第2课:让网页“活”起来 —— JSP是什么?
java·开发语言·前端·后端·web
醇氧2 小时前
【Linux】Java 服务生产级部署指南:实现常驻后台、开机自启与系统服务化管理
java·开发语言
回忆2012初秋2 小时前
【Nginx】优雅地走进高性能 Web 服务器世界(1)
服务器·前端·nginx