当前位置:网站首页>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 )
边栏推荐
- Reading notes of Clickhouse principle analysis and Application Practice (3)
- Who are you and I
- clickhouse原生監控項,系統錶描述
- CA I ah, several times Oh, ah, a sentence IU home Oh
- Prediction and regression of stacking integrated model
- Oprator-1 first acquaintance with oprator
- 【回溯】全排列 II leetcode47
- 电子方案开发——智能跳绳方案
- [untitled]
- A group of K inverted linked lists
猜你喜欢

1-2 install and configure MySQL related software

Personal developed penetration testing tool Satania

Radar data processing technology

Phoenix architecture: an architect's perspective

【无标题】

根据肠道微生物组重新思考健康饮食

Reading notes of Clickhouse principle analysis and Application Practice (1)
笔记【JUC包以及Future介绍】

How to move forward when facing confusion in scientific research? How to give full play to women's advantages in scientific research?
Understand what MySQL index push down (ICP) is in one article
随机推荐
文本生成模型退化怎麼辦?SimCTG 告訴你答案
时空数据挖掘:综述
SQL server extracts pure numbers from strings
数字货币:影响深远的创新
兴奋神经递质——谷氨酸与大脑健康
Adobe Photoshop (PS) - script development - remove file bloated script
vim 常用快捷键
ICML2022 | 序列决策的效用理论
Double solid histogram / double y-axis
Markdown notes concise tutorial
ca i啊几次哦啊句iu家哦11111
PyTorch量化感知训练(QAT)步骤
“信任机器”为发展赋能
【回溯】全排列 II leetcode47
Flutter 嵌套地狱?不存在的,ConstraintLayout 来解救!
Test medal 1234
Go build server Foundation
申请Vector 总线协议彩图壁纸挂画,非常棒哦!
What about degradation of text generation model? Simctg tells you the answer
漫谈Clickhouse Join