当前位置:网站首页>Options request (cross domain pre check)
Options request (cross domain pre check)
2022-06-13 12:24:00 【Mirage hope】
summary
options A request is a pre check request , Can be used to detect what the server allows http Method . When a cross domain request is initiated , For safety reasons , When a certain condition is triggered, the browser will automatically initiate before the formal request OPTIONS request , namely CORS Pre inspection request , If the server accepts the cross domain request , The browser continues to make formal requests .
One 、 What is? options request
HTTP Of OPTIONS Method Used to get the communication options supported by the destination resource . The client can URL Use OPTIONS Method , Or the whole station ( By way of URL Set to "*") Using this method .( In short , It can be used options Request to sniff which request method is supported in the corresponding server ).
reason
This is because in a cross domain situation , Launch in browser " Complex request " It was initiated on its own initiative . Cross domain sharing standards and specifications require , For those that may have side effects on server data HTTP Request method ( especially GET Outside of the HTTP request , Or something MIME Type of POST request ), Browsers must first use OPTIONS Method to initiate a pre check request (preflight request), So we can know whether the server allows the cross domain request . After the server confirms the permission , To launch the actual HTTP request .
Two 、 Simple requests and complex requests
Some requests don't trigger CORS Pre inspection request , Such a request is generally called “ A simple request ” , The request that will trigger the pre inspection is “ Complex request ” .
A simple request
- The request method is
GET、HEAD、POSTTime's request ;- Consider setting the header field within the specification set , Such as
Accept/Accept-Language/Content-Language/Content-Type/DPR/Downlink/Save-Data/Viewport-Width/Width;- Content-Type Is limited to one of the following three values , namely
application/x-www-form-urlencoded、multipart/form-data、text/plain;- Arbitrary in the request
XMLHttpRequestUploadObject does not register any event listeners ;- Not used in the request
ReadableStreamobject .
Complex request
- PUT/DELETE/CONNECT/OPTIONS/TRACE/PATCH;
- Artificially set the header field outside of the following set , That is, the fields outside the simple request ;
- Content-Type The value of is not one of the following , namely application/x-www-form-urlencoded、multipart/form-data、text/plain.
request header Key fields for
response header Key fields for
3、 ... and 、Options Request to optimize
When a cross domain request is initiated , A simple request initiates only one request ; Complex requests require 2 Time , First initiate options request , Confirm whether the target resource supports cross domain , The browser will respond according to the server header Automatically process the remaining requests , If the response supports cross domain , Then continue to make normal requests ; If you don't support it , An error will be displayed on the console .
therefore , When the preview is triggered , Cross domain requests are sent 2 Requests , Increase the number of requests , meanwhile , It also delays when the request actually starts , Can seriously affect performance .
Optimize options Two ways to request :
Method 1 : Use other cross domain methods to make cross domain requests , Convert complex requests to simple requests , such as JSONP etc. ;
Method 2 : Yes options Request to cache .
Server settings Access-Control-Max-Age Field , So when you first request that URL Will be issued when OPTIONS request , The browser will respond to the Access-Control-Max-Age Field to cache the name of the request OPTIONS The response result of the pre check request ( The specific cache time also depends on the default maximum supported by the browser , Take the minimum of both , It's usually 10 minute ). Within the cache validity period , The request for the resource (URL and header If the fields are the same ) It's not going to trigger a preview .(chrome Open the console and you can see , When the server responds Access-Control-Max-Age Only the first request has a preview , Not in the back . Be careful to turn on caching , Get rid of disable cache Check .)
The article citations :https://javajgs.com/archives/97325
边栏推荐
- 2022年二建《公路》科目答案已出,请收好
- The beginning of everything, test girl
- 陈宏智:字节跳动自研万亿级图数据库ByteGraph及其应用与挑战
- Best practices for SaaS application architecture
- 内部寄存器类型
- 如何基于Ceph设计与构建一套软件定义存储系统
- Talk to CTO: how to avoid getting into management dilemma | Q recommendation
- Machine learning (III) - LDA (linear discriminant analysis) theory and code explanation
- 7. Introduction to field sensing decomposing machine FFM
- 二分法及对数器
猜你喜欢

机器学习服务助应用内文本语种在线和离线检测

2022年二建《建筑》参考答案汇总

Based on STM32F103 - DS1302 date time + serial port printing

陈宏智:字节跳动自研万亿级图数据库ByteGraph及其应用与挑战

CVPR2022 | A ConvNet for the 2020s & 如何设计神经网络总结

14. Notes on using border decorator of WPF

Analysis of DuPont analysis method: financial analysis of the New Retail Group Co., Ltd

9、Wide&Deep简介

Review guide for students

Pulsar producer
随机推荐
Interview shock 56: what is the difference between clustered index and non clustered index?
Problems encountered in using the Pluto table of the flutter plug-in
Envoyer un SMS - système de carte d'accès intelligent basé sur stm32f103 + as608 module d'empreintes digitales + clé matricielle 4x4 + sim900a
9、Wide&Deep简介
SMS based on stm32f103+as608 fingerprint module +4x4 matrix key +sim900a - intelligent access control card system
Camunda定时器事件示例Demo(Timer Events)
Review guide for students
Interview shock 56: what is the difference between clustered index and non clustered index?
基于STM32F103——矩阵按键+串口打印
基于三维GIS技术的行业发展及研究现状
9. Introduction to wide & deep
我们的B端SaaS为什么生存得如此艰难
高光谱真彩色图像合成原理及具体操作过程
That is to say, it launched the industry's first data stream recording PAAS scheme, which can reproduce the recording capacity of large manufacturers at low cost
5 locksupport and thread interruption
This article clearly explains the design of authority management in tob SaaS system
flutter 插件 Pluto表格使用所遇问题
UNIX环境高级编程(APUE,第三版)---环境搭建
What if the second construction fails to pass the post qualification examination? This article tells you
如何基于Swift开源技术构建云存储集群

