• <bdo id='pRUPB'></bdo><ul id='pRUPB'></ul>
    <i id='pRUPB'><tr id='pRUPB'><dt id='pRUPB'><q id='pRUPB'><span id='pRUPB'><b id='pRUPB'><form id='pRUPB'><ins id='pRUPB'></ins><ul id='pRUPB'></ul><sub id='pRUPB'></sub></form><legend id='pRUPB'></legend><bdo id='pRUPB'><pre id='pRUPB'><center id='pRUPB'></center></pre></bdo></b><th id='pRUPB'></th></span></q></dt></tr></i><div id='pRUPB'><tfoot id='pRUPB'></tfoot><dl id='pRUPB'><fieldset id='pRUPB'></fieldset></dl></div>
  • <legend id='pRUPB'><style id='pRUPB'><dir id='pRUPB'><q id='pRUPB'></q></dir></style></legend>

    1. <small id='pRUPB'></small><noframes id='pRUPB'>

      <tfoot id='pRUPB'></tfoot>

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

        What is the point of request.mode in the fetch API, especially with respect to cors?(fetch API 中的 request.mode 有什么意义,尤其是在 cors 方面?)
        • <bdo id='bpnZk'></bdo><ul id='bpnZk'></ul>
          <tfoot id='bpnZk'></tfoot>
            <tbody id='bpnZk'></tbody>
          <legend id='bpnZk'><style id='bpnZk'><dir id='bpnZk'><q id='bpnZk'></q></dir></style></legend>
          • <i id='bpnZk'><tr id='bpnZk'><dt id='bpnZk'><q id='bpnZk'><span id='bpnZk'><b id='bpnZk'><form id='bpnZk'><ins id='bpnZk'></ins><ul id='bpnZk'></ul><sub id='bpnZk'></sub></form><legend id='bpnZk'></legend><bdo id='bpnZk'><pre id='bpnZk'><center id='bpnZk'></center></pre></bdo></b><th id='bpnZk'></th></span></q></dt></tr></i><div id='bpnZk'><tfoot id='bpnZk'></tfoot><dl id='bpnZk'><fieldset id='bpnZk'></fieldset></dl></div>

              1. <small id='bpnZk'></small><noframes id='bpnZk'>

                  本文介绍了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 方面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                  Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                  How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                  How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                  How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                  How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)

                  <small id='9EgT1'></small><noframes id='9EgT1'>

                          <tfoot id='9EgT1'></tfoot>
                          • <bdo id='9EgT1'></bdo><ul id='9EgT1'></ul>
                          • <legend id='9EgT1'><style id='9EgT1'><dir id='9EgT1'><q id='9EgT1'></q></dir></style></legend>

                              <tbody id='9EgT1'></tbody>

                            <i id='9EgT1'><tr id='9EgT1'><dt id='9EgT1'><q id='9EgT1'><span id='9EgT1'><b id='9EgT1'><form id='9EgT1'><ins id='9EgT1'></ins><ul id='9EgT1'></ul><sub id='9EgT1'></sub></form><legend id='9EgT1'></legend><bdo id='9EgT1'><pre id='9EgT1'><center id='9EgT1'></center></pre></bdo></b><th id='9EgT1'></th></span></q></dt></tr></i><div id='9EgT1'><tfoot id='9EgT1'></tfoot><dl id='9EgT1'><fieldset id='9EgT1'></fieldset></dl></div>