基于 Web 的前后端数据传输
source link: https://s.ourai.ws/notes/data-transmission-in-web-based-apps/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
XMLHttpRequest(XHR)
Fetch
与 XHR 不同的是,fetch()
会返回一个 Promise,更加符合现代前端的异步编程模型:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width" />
<title>Fetch basic example</title>
<link rel="stylesheet" href="" />
</head>
<body>
<h1>Fetch basic example</h1>
<img src="" class="my-image" />
</body>
<script>
const myImage = document.querySelector(".my-image");
fetch("flowers.jpg")
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error, status = ${response.status}`);
}
return response.blob();
})
.then((myBlob) => {
const objectURL = URL.createObjectURL(myBlob);
myImage.src = objectURL;
})
.catch((error) => {
const p = document.createElement("p");
p.appendChild(document.createTextNode(`Error: ${error.message}`));
document.body.insertBefore(p, myImage);
});
</script>
</html>
在 fetch()
刚被支持时,已经发出去且尚未完成的请求是无法被取消的,但在 2017、2018 年时各大浏览器陆续支持通过第二个参数传入 signal
中止请求:
let controller;
const url = "video.mp4";
const downloadBtn = document.querySelector(".download");
const abortBtn = document.querySelector(".abort");
downloadBtn.addEventListener("click", fetchVideo);
abortBtn.addEventListener("click", () => {
if (controller) {
controller.abort();
console.log("Download aborted");
}
});
function fetchVideo() {
controller = new AbortController();
const signal = controller.signal;
fetch(url, { signal })
.then((response) => {
console.log("Download complete", response);
})
.catch((err) => {
console.error(`Download error: ${err.message}`);
});
}
WebSocket
Web Workers
Recommend
-
80
数据传递转码 数据在传输的过程中,浏览器会对数据进行编码,假如我现在有一条数据 {"name": "测试"},如果我们通过 get 方法传递数据,这条数据会被拼接到 url 请求的后面,如:localhost:8080/src/text.html?name=测试。 uri本身是采用ASCII编码的,所以如果是非...
-
65
本文经机器之心(微信公众号:almosthuman2014)授权转载 选自the Verge 作者:
-
62
在《圣经》的故事中,曾经描写过人类为了去往天堂,一起合力建造可以触到天空的通天塔。为了阻止这一计划,上帝从此让人类说上不同的语言,最后人类就失去了合力造塔的能力。 今天的互联网企业发展也是一样,数据作为技术的最大...
-
73
SD-WAN与人工智能是目前大家谈论最多的话题,但它们在快速云备份和恢复方面存在着局限性。在SD-WAN出现之前,传统的广域网的所有应用、带宽拥塞和高质量的服务(QoS)都必须通过一个管道使用多协议标签交换(MPLS)将每个分支...
-
45
本文首发于 vivo 互联网技术微信公众号 ( https://mp.weixin.qq.com/s/EBaUiMim6LjVHQy2-dCDOg ) 作者:周建军 本文根据周建军在 20...
-
60
-
43
Canal的介绍 Canal的历史由来 在早期的时候,阿里巴巴公司因为杭州和美国两个地方的机房都部署了数据库实例,但因为跨机房同步数据的业务需求 ,便孕育而生出了Canal,主要是基于trigger(触发器)的方式获取增量变...
-
62
根据 The Verge 报告,苹果加入了开源数据传输项目 Data Transfer Project。Data Transfer Project 在 2018 年上线,旨在创建一个开源的服务到服务数据可移植平台,以便其网站用户和其他人可以轻松将数据从一个平台迁移到另一个平台。它提...
-
8
简单就是美。在网络协议的世界中,TCP和UDP是建立在IP协议基础上的两个非常通用的协议。我们现在经常使用的HTTP协议就是建立在TCP协议的基础上的。相当于TCP的稳定性来说,UDP因为其数据传输的不可靠性,所以用在某些特定的场合,如直播、广播消息、视频音频流...
-
4
仙工AMB系列无人搬运底盘 Windows 10 IntelliJ IDEA Android Studio 首先底盘控制器和Web后端通过TCP协议进行数据交互,所有的逻辑处理均在Web后端中实现,所有产生的交互数据均...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK