跨域 cors 请求两次

The story of requesting twice, allow me to explain how it all began.

请求两次的故事,让我解释一下这是如何开始的。

While working on a feature, I decided to look at the network tab and observed that the first request was sent with method OPTIONS, and the following request after it was the request with the correct method eg GET, POST etc, which is returning the expected payload. Basically two calls for the same request.

在使用某个功能时,我决定查看“网络”选项卡,并观察到第一个请求是使用方法OPTIONS发送的,随后的请求是使用正确方法(例如GET,POST等)的请求,该请求正在返回预期的结果有效载荷。 基本上是两个请求相同的请求。

Here take a look at the screen shots below

这里看看下面的屏幕截图

After digging few docs, I realised it was an expected behaviour. It is related to the concept of HTTP access control (CORS).

挖掘了几篇文档后,我意识到这是一种预期的行为。 它与HTTP访问控制(CORS)的概念有关。

To understand it better, let me explain a bit about CORS and requests.

为了更好地理解它,让我解释一些有关CORS和请求的信息。

HTTP访问控制(CORS) (HTTP access control (CORS))

Cross-Origin Resource Sharing (CORS) is a mechanism that uses additional HTTP headers to let a user agent gain permission to access selected resources from a server on a different origin (domain) than the site currently in use.

跨域资源共享( CORS )是一种机制,它使用附加的HTTP标头来使用户代理获得从与当前使用的站点不同的源(域)上的服务器访问选定资源的权限。

Let us understand the above image above to get a better understanding of CORS.

让我们了解上面的图片,以更好地了解CORS。

  1. Same Origin Request: We have opened domain-a.com, where we are requesting a blue image hosted in web server domain-a.com. Since we are performing our requests in the same domain, it is called a Same-origin request.

    相同来源请求:我们已经打开domain-a.com ,在这里我们请求托管在Web服务器domain-a.com中蓝色图像 由于我们在同一个域中执行请求,因此称为“同源请求”。

  2. Cross Origin Request: We have opened domain-a.com, where we are requesting a red image hosted in web server domain-b.com. Since we are performing our requests in different domains, it is called a Cross-origin Request.

    跨源请求:我们已经打开domain-a.com ,在此我们请求托管在Web服务器domain-b.com中红色图像 由于我们在不同的域中执行请求,因此称为跨源请求。

简单的要求 (Simple requests)

These are requests that doesn't send it’s first request as method OPTIONS. It is fired only once.

这些请求不会将其第一个请求作为方法OPTIONS发送。 它仅发射一次。

Surely it begs the question, why will the first request have method OPTIONS if we are not sending it, please have patience it will be explained below in preflight section ☺

毫无疑问,这是一个问题,如果我们不发送第一个请求,为什么第一个请求会有方法OPTIONS,请耐心等待,这将在以下预检部分section中进行说明。

But before that let us understand what are the points that make request simple.

但是在此之前,让我们了解使请求变得简单的要点。

  1. The only allowed methods in simple request are:简单请求中唯一允许的方法是:
  • GET

    得到

  • HEAD

  • POST

    开机自检

2. Apart from the headers set automatically by the user agent (for example, connection , User-Agent, or any of the other headers with names defined in the Fetch spec as a “forbidden header name”), the only headers which are allowed to be manually set are those which the Fetch spec defines as being a “CORS-safelisted request-header”, which are:

2.除了由用户代理自动设置的标头(例如,connection, User-Agent或其他名称在Fetch规范中定义为“禁止标头名”的标头)外,仅允许使用标头可以手动设置的是Fetch规范定义为“ CORS安全列出的请求标头”的那些标头 ,它们是:

  • Accept接受
  • Accept-Language接受语言
  • Content-Language内容语言
  • Content-Type内容类型
  • DPRDPR
  • Downlink下行链接
  • Save-Data保存数据
  • Viewport-Width视口宽度
  • Width宽度

3. The only allowed values for the Content-Type header are:

3. Content-Type标头的唯一允许值为:

  • application/x-www-form-urlencoded应用程序/ x-www-form-urlencoded
  • multipart/form-data多部分/表单数据
  • text/plain文字/纯文字

4. No event listeners are registered on any XMLHttpRequestUpload object used in the request.

4.没有在事件中使用的任何XMLHttpRequestUpload对象上注册事件侦听器。

5. No ReadableStream object is used in the request.

5.请求中未使用ReadableStream对象。

事前要求 (Preflighted requests)

Preflighted request is a type of request which sends an HTTP request by the OPTIONS method to the resource on the other domain, in order to determine whether the actual request is safe to send. Cross-site requests are preflighted like this since they may have implications to user data. It is evident from the screenshots above.

预检请求是一种请求,它通过OPTIONS方法将HTTP请求发送到另一个域上的资源,以确定实际请求是否可以安全发送。 跨站点请求这样被预检,因为它们可能会影响用户数据。 从上面的截图可以明显看出。

For requests like PUT, DELETE, PATCH etc, preflight requests are sent.

对于PUT,DELETE,PATCH等请求,将发送预检请求。

Below flowchart summarises really well how it works.

下面的流程图很好地总结了它是如何工作的。

This flowchart opens up a door to a whole new knowledge. Couldn’t help but appreciate how good it is!

该流程图为全新的知识打开了一扇门。 不禁感激它有多棒!

Strangely enough even GET request was observed to have preflights which for my case was due to presence of custom header Authorization, which can be seen from the screenshot below

奇怪的是,即使是GET请求也被发现有预检,对于我而言,这是由于存在自定义标头授权,可以从下面的屏幕截图中看到

飞行前要求不好吗? (Is Preflight request bad?)

It is a small request without a body, but I always felt it as a bother. It is still a request, and each request is a cost, no matter how small that request is, so I definitely recommend to try and avoid having such cases.

没有身体这是一个很小的要求,但是我一直觉得这很麻烦。 它仍然是一个请求,每个请求都是成本,无论该请求有多小,所以我绝对建议尝试避免此类情况。

我们如何避免呢? (How do we avoid it?)

Well the easiest solution is avoid CORS, try to keep our resources and APIs in the same domain. It is really that simple.

最简单的解决方案是避免使用CORS,请尝试将我们的资源和API放在同一域中。 真的就是这么简单。

结论 (Conclusion)

It is always good to be armed with knowledge of how requests work. Even if the cost is very low, it still matters. Saving small requests can make our application really fast in the long run. Well I believe in the future, which is fast and furious.

知道请求如何工作总是很好。 即使成本很低,它仍然很重要。 从长远来看,保存小的请求可以使我们的应用程序真正更快。 好吧,我相信未来,这是快速而愤怒的。

Follow me on twitter to get more updates regarding new articles and to stay updated in latest frontend developments. Also share this article on twitter to help others know about it. Sharing is caring ^_^

跟我来 Twitter以获得有关新文章的更多更新,并保持最新的前端开发更新。 可以在Twitter上分享此文章,以帮助其他人了解它。 分享是关怀^ _ ^

一些有用的资源 (Some helpful resources)

Below are some of the links that inspired this article

以下是一些启发本文的链接

  1. https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

    https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CORS

  2. https://stackoverflow.com/questions/24704638/options-request-makes-application-2x-slower

    https://stackoverflow.com/questions/24704638/options-request-makes-application-2x-slower

  3. https://stackoverflow.com/questions/29954037/why-is-an-options-request-sent-and-can-i-disable-it/29954326

    https://stackoverflow.com/questions/29954037/why-is-an-options-request-sent-and-can-i-disable-it/29954326

  4. https://www.html5rocks.com/en/tutorials/cors/

    https://www.html5rocks.com/zh-CN/tutorials/cors/

翻译自: https://www.freecodecamp.org/news/the-story-of-requesting-twice-cors/

跨域 cors 请求两次

跨域 cors 请求两次_请求两次的故事-CORS相关推荐

  1. KONG (API网关) 用CORS处理跨域,针对:非简单请求

    研究了挺久的,从不知道什么是跨域到最终解决问题. 简单说,会产生跨域的异常主要是当前页面域名与页面内直接请求的域名不是同一个域,浏览器就是拦截这种它认为不安全的操作 首先必须明确一点:跨域有两种(1. ...

  2. 因跨域,post请求变options请求(vue)

    1.options是什么? options在此问题中属于第二种,当涉及到跨域时,并且是post请求时,本地服务器会先发送一个options请求到服务器,如果服务器认为options请求时无危险性且认可 ...

  3. JavaScript跨域问题分析与总结_直来直往_百度空间

    JavaScript跨域问题分析与总结_直来直往_百度空间 JavaScript跨域问题分析与总结 2009-11-15 16:44 一.为什么需要JS跨域 假设我们构建了一个网上商城www.xxx. ...

  4. Atitit.js跨域解决方案attilax大总结 后台java php c#.net的CORS支持

    Atitit.js跨域解决方案attilax大总结 后台java php c#.net的CORS支持 1.设置 document.domain为一致  推荐1 2.Apache 反向代理 推荐1 3. ...

  5. authorization 传 就跨域_跨域访问接口上传图片出现options请求问题解决方法

    1.首先因为服务器端会先发送一个option请求到后台  在后台返回一个post给页面 页面在处理post请求给接口 2.先写一个过滤器, 我们自己定义一个过滤器 package com.adtime ...

  6. Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据

    场景 网络接口返回json格式数据,可以直接在浏览器中访问. 可以在页面上右键另存为json格式文件 怎样在本地使用nginx配置,模拟get接口返回该json文件,使前端使用ajax请求数据时,可以 ...

  7. vue-cli设置跨域代理 + 开发/生成环境简单请求接口设置

    1.设置dev.proxyTable实现开发环境的跨域代理: 在config文件夹下index.js文件内: 1 module.exports = { 2 dev: { 3 proxyTable: { ...

  8. 什么是跨域?浏览器为何禁止跨越请求?如何解决浏览器跨越问题

    1.什么是跨域? 跨越是产生于浏览器的"同源策略",所谓同源策略是指: 1)协议:http.https 2)域名:www.baidu.com.uland.taobao.com - ...

  9. Chrome98和Chrome101的跨域变化,httpOPTIONS预检请求,私有网络访问限制

    在Chrome94更新时,发现访问本地服务器的时候谷歌浏览器限制了访问本地资源 当时通过一个浏览器设置进行了处理.但是治标不治本,98版本更新后又出现了CORS跨域问题.查询了一下资料: Chrome ...

最新文章

  1. JavaBean规范
  2. 文件和存储管理学习笔记-动态磁盘管理
  3. 学习,编译ffmpeg tutorial
  4. session mysql登录实现_PHP+MYSQL+MYSQL+SESSION实现用户登录的实例
  5. python刷题相关资料汇总(二)
  6. 基本概念---part5
  7. 51nod1297 管理二叉树
  8. Cream Finance关于提高三个v2 yVaults的质押系数的提案已经完成
  9. 刷新按钮_不能忍:用户求微软为Win10 Wi-Fi窗口添加刷新按钮
  10. 深度学习 --- 卷积神经网络CNN(LeNet-5网络学习算法详解)
  11. 银联 php hex2bin,银联支付
  12. ESP8266 在Arduino 使用ST7789 OLED
  13. C# MessageBox 确定|取消
  14. printf二进制数据
  15. MongoSocketOpenException: Exception opening socket
  16. 和大学说再见,却不跟青春道别
  17. 微信小程序案例php,微信小程序实现删除处理的案例
  18. 软件测试工程师工资有多高?
  19. c语言画板,简单的像素画板(C语言编写)
  20. 用Python输出三角形图案

热门文章

  1. 【今日CV 视觉论文速览】 7 Feb 2019
  2. ECCV2018--点云匹配
  3. 打印出不同顺序的字符串单引号和双引号的差异
  4. 事务相关命令 mysql
  5. 笔记 备考2022华师大 教育专硕
  6. 媒体查询 200304
  7. 爬虫-13-认识代理
  8. django-后台管理-表显示相关
  9. javascript-注释-字符串数据类型的方法
  10. 编辑工具-sublime使用介绍