当前位置:网站首页>1-19 using CORS to solve interface cross domain problems
1-19 using CORS to solve interface cross domain problems
2022-06-30 21:32:00 【Endless pie】
utilize CORS Solve the problem of interface Cross Domain
GET and POST Interface , There is a very serious problem : Cross domain requests are not supported .
There are two main solutions to interface cross domain problems :
- CORS( Mainstream solutions , Recommended )
cors yes Express A third-party middleware . Through installation and configuration cors middleware , It can easily solve cross domain problems .
The steps used are divided into the following three steps :
1. function npm install cors Install middleware
2. Use const cores = require(‘cors’) Import Middleware
3. Call before routing app.use(cors()) Configuration middleware
What is? CORS Cross-domain resource sharing ?
COR(cross-Origin Resource Sharing, Cross-domain resource sharing ) By a series of HTTP The response header consists of , these HTTP The response header determines whether the browser blocks the front end JS Code gets resources across domains .
The browser's same origin security policy blocks web pages by default “ Cross domain ” Access to resources . But if the interface server is configured CORS dependent HTTP Response head , You can access the cross domain access restrictions on the browser side .
(1)CORS The response header can carry a Access-Control-Allow-Origin
If there is Access-Control-Allow-Origin The value of the field is a wildcard *, Indicates that requests from any domain are allowed , The sample code is as follows :
res.setHeader('Access-Control-Allow-Origin','*')
(2) By default ,CORS Only the client can send the following message to the server 9 A request header :
Accept、Accept-Language、Content-Language、DPR、Downlink、Save-Data、Viewport-Width、Width、Content-Type( Values are limited to text/plain、mutipart、from-data、application/x-www-form-urlencoded One of the three )
If the client sends additional request header information to the server , On the server side , adopt Access-Control-Allow-Headers Declare additional request headers , Otherwise, this request will fail
(3) Response head Access-Control-Allow-Methods
By default ,CORS Only client initiated GET、POST、HEAD request .
If the client wants to pass PUT、DELETE And so on , On the server side , adopt Access-Control-Alow-Methods To indicate what is allowed by the actual request HTTP Method .
// Only supported by default POST,GET,DELETE Request method
// Only POST,GET,DELETEM,HEAD Request mode
res.setHeader('Access-Control-Alow-Methods','POST,GET,DELETEM,HEAD')
// Allow all HTTP Request method
res.setHeader('Access-Control-Alow-Methods','POST,GET,DELETEM,HEAD')
const cors = require('cors')
app.use(cors())
- JSONP( Flawed solutions : Only support GET request )
边栏推荐
- Move blog to CSDN
- Clickhouse distributed table engine
- 1-7 path module
- CA I ah, how many times Oh, ah sentence IU home Oh 11111
- 1-7 Path路径模块
- Electronic scheme development - Intelligent rope skipping scheme
- Auto-created primary key used when not defining a primary key
- 文本生成模型退化怎麼辦?SimCTG 告訴你答案
- 网络营销之四大误解
- FreeRTOS record (IX. an example of a bare metal project transferring to FreeRTOS)
猜你喜欢

全面认识痛风:症状、风险因素、发病机理及管理

3Ds Max 精模obj模型导入ArcGIS Pro (二)要点补充

asp. Net core JWT delivery

How to move forward when facing confusion in scientific research? How to give full play to women's advantages in scientific research?

Why have the intelligent investment advisory products collectively taken off the shelves of banks become "chicken ribs"?

1-2 install and configure MySQL related software

Reading notes of Clickhouse principle analysis and Application Practice (2)

申请Vector 总线协议彩图壁纸挂画,非常棒哦!

Radar data processing technology

布隆过滤器
随机推荐
ca i啊几次哦啊句iu家哦11111
1-13 express listens to get and post requests & processes requests
开发属于自己的包
MySQL batch update
What happens when word encounters an error while trying to open a file?
mysql-批量更新
Spatiotemporal data mining: an overview
Radar data processing technology
MySQL advanced 3
PyTorch量化感知训练(QAT)步骤
CA I ah, how many times Oh, ah sentence IU home Oh 11111
1-12 preliminary understanding of Express
侧睡哈哈哈哈
Understand what MySQL index push down (ICP) is in one article
1-14 express托管静态资源
clickhouse原生监控项,系统表描述
1-17 express中间件
How to run jenkins build, in multiple servers with ssh-key
. NETCORE redis geo type
Electronic scheme development - Intelligent rope skipping scheme