fetch API 中的 request.mode 有什么意义,尤其是在 cors 方面?

2023-09-05前端开发问题
38

本文介绍了fetch API 中的 request.mode 有什么意义,尤其是在 cors 方面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

查看新的 fetch API,您可以在请求中指定模式字段.来自 Mozilla:

Looking at the new fetch API, you can specificy a mode field in the request. From Mozilla:

The mode read-only property of the Request interface contains the mode 
of the request (e.g., cors, no-cors, same-origin, or navigate.) This is 
used to determine if cross-origin requests lead to valid responses, and 
which properties of the response are readable.

然后是如何使用它:

var myHeaders = new Headers();

var myInit = { method: 'GET',
           headers: myHeaders,
           mode: 'cors',
           cache: 'default' };

fetch('flowers.jpg', myInit).then(function(response) {
  return response.blob();
}).then(function(myBlob) {
  var objectURL = URL.createObjectURL(myBlob);
  myImage.src = objectURL;
});

我不明白您为什么需要具体说明在请求本身中如此不言自明的内容?如果我从客户端网站 http://foo.com 服务器请求资源/client.com" rel="noreferrer">http://client.com,服务器没有源头可以看吗?模式不会只会引起混乱吗?

I don't understand why would you would need to specificy something that is so self evident in the request itself? If I was requesting a resource from http://foo.com servers on the client website http://client.com, doesn't the server have the origin header to look at? And wouldn't the mode only cause confusion?

此外,我想弄清楚 mode: same-origin 甚至可以达到什么目标?您可以使用它来确保始终向您的源发出请求." 但是,如果您正在发送请求,或者如果其他开发人员有权访问代码,为什么不你只是不发送请求而不是一个字段来表明它是无效的?

Furthermore I'm trying to figure out what goal mode: same-origin would even serve? "You could use this to ensure that a request is always being made to your origin." But if you're sending a request out, or if another developer has access to the code, why wouldn't you just not send the request instead of a field to indicate it's invalid?

推荐答案

Fetch API 在设计上公开了浏览器内部用于获取的相同原语.

The Fetch API by design exposes the same primitives that browsers use for fetches internally.

mode 是这些原语之一.而且它不仅仅用于 Fetch 规范中定义 Fetch API 的部分——因为 Fetch 规范除了定义 JavaScript API 之外,还定义了浏览器如何在内部处理各种类型的获取的低级算法.

mode is one of those primitives. And it’s not just used in the part of the Fetch spec that defines the Fetch API — because the Fetch spec, along with defining that JavaScript API, also defines low-level algorithms for how browsers handle fetches of various kinds internally.

在 HTML 规范等规范中定义的浏览器内部算法引用了 Fetch 规范中的那些低级算法,并依赖于设置 mode 和 fetch 的其他方面.

Browser-internal algorithms defined in specs such as the HTML spec reference those low-level algorithms in the Fetch spec, and rely on setting the mode and other aspects of the fetch.

例如,在 HTML 规范中,获取一个经典的工作脚本 算法开始于:

For example, in the HTML spec, the fetch a classic worker script algorithm starts with this:

获取一个经典的工作脚本,给定一个 url,一个 fetch客户端设置对象目的地脚本设置对象,运行这些步骤.该算法将使用 null(失败时)或新的异步完成经典脚本(成功时).

To fetch a classic worker script given a url, a fetch client settings object, a destination, and a script settings object, run these steps. The algorithm will asynchronously complete with either null (on failure) or a new classic script (on success).

  1. request成为一个新的requesturl 是 url,client 是获取客户端设置对象,destination 是 destination,mode 是same-origin", 凭证模式是same-origin",解析器metadata 是not parser-inserted",并且其 use-URL-credentials flag 已设置.
  1. Let request be a new request whose url is url, client is fetch client settings object, destination is destination, mode is "same-origin", credentials mode is "same-origin", parser metadata is "not parser-inserted", and whose use-URL-credentials flag is set.

注意 Let 请求 成为一个新的 请求模式 是same-origin" 部分 - 请注意 请求 转到 https://fetch.spec.whatwg.org/#concept-request 和 mode 的超链接 转到 https://fetch.spec.whatwg.org/#概念请求模式.

Note the Let request be a new request and mode is "same-origin" parts —and notice that the hyperlink for request goes to https://fetch.spec.whatwg.org/#concept-request and the hyperlink for mode goes to https://fetch.spec.whatwg.org/#concept-request-mode.

所以 HTML 规范需要same-origin".请求的模式设置——为了指定浏览器在进行某种类型的获取时在内部使用的行为.

So the HTML spec has need for the "same-origin" mode setting for requests — in order to specify behavior that browsers use internally when making a certain type of fetch.

这就是为什么 Fetch 规范需要为 fetch 提供特定模式的原因.除此之外,Fetch API 提供了设置same-origin"功能的原因.模式(如上所述)与允许您作为 Web 开发人员访问相同原语的目标保持一致(如上所述)在进行提取时,浏览器可以在内部访问这些原语.

And that kind of thing is why the Fetch spec needs to provide for fetches to have particular modes. Along with that, the reason the Fetch API provides the ability to set the "same-origin" mode is (as mentioned above) in keeping with the goal of allowing you as a web developer to have access to the same primitives browsers have access to internally when making fetches.

您可能永远不会发现需要 Fetch API 公开的所有各种模式 - 您不太可能想要使用 navigate 模式 - 但它们都在那里,因为它们表示任何给定提取场景所需的完整模式集(包括仅可能由浏览器内部使用的场景).

You may not ever find need for all the various modes the Fetch API exposes — it’s pretty unlikely you’d ever want to use the navigate mode —but they’re all there nonetheless, because they represent the complete set of modes that are needed for any given fetch scenario (including scenarios that are only ever likely to be used by browsers internally).

这篇关于fetch API 中的 request.mode 有什么意义,尤其是在 cors 方面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13