当前位置:网站首页>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 )
边栏推荐
- Understand what MySQL index push down (ICP) is in one article
- 侧睡哈哈哈哈
- Personal developed penetration testing tool Satania
- ca i啊几次哦啊句iu家哦
- Internet of things botnet gafgyt family and backdoor vulnerability exploitation of Internet of things devices
- MySQL advanced 3
- Go build server Foundation
- 1-13 express listens to get and post requests & processes requests
- 时空数据挖掘:综述
- Dm8: generate DM AWR Report
猜你喜欢

多态在代码中的体现

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

Four Misunderstandings of Internet Marketing

Arcmap|assign values to different categories of IDS with the field calculator

【无标题】

MySQL advanced 3

Double solid histogram / double y-axis

文本生成模型退化怎么办?SimCTG 告诉你答案

興奮神經遞質——穀氨酸與大腦健康

全面认识痛风:症状、风险因素、发病机理及管理
随机推荐
Text recognition svtr paper interpretation
qsort函数和模拟实现qsort函数
Metauniverse may become a new direction of Internet development
Understanding polymorphism
1-19 利用CORS解决接口跨域问题
Why have the intelligent investment advisory products collectively taken off the shelves of banks become "chicken ribs"?
《ClickHouse原理解析与应用实践》读书笔记(1)
测试勋章1234
Markdown notes concise tutorial
将el-table原样导出为excel表格
1-1 数据库的基本概念
Introduction of 3D Max fine model obj model into ArcGIS pro (II) key points supplement
Go build server Foundation
申请Vector 总线协议彩图壁纸挂画,非常棒哦!
Encryption and decryption and the application of OpenSSL
To the Sultanate of Anderson
Five years after graduation, I wondered if I would still be so anxious if I hadn't taken the test
Adobe Photoshop (PS) - script development - remove file bloated script
What about degradation of text generation model? Simctg tells you the answer
1-21 JSONP接口