当前位置:网站首页>What is cross domain? How to solve the cross domain problem?
What is cross domain? How to solve the cross domain problem?
2022-07-28 09:29:00 【Front end program】
List of articles
Tips: Write Vue Too many projects , Proxy server has been used for cross domain . Today in review node When , It also summarizes the back-end methods to solve browser cross domain
List of articles
- List of articles
- One 、 What is cross-domain ?
- Two 、Vue The proxy server resolves requests across domains
- 3、 ... and 、 Tools to solve cross domain
- Four 、Node Solving cross domain problems
- 5、 ... and 、HTML Label or Jquery To solve the cross domain (jsonp Cross domain )
- summary
One 、 What is cross-domain ?
1. Introduction to the cross domain and homology strategy of the browser
Cross domain , It means that the browser can't execute scripts of other websites .Cross domain is caused by the same origin policy of the browser , Security restrictions imposed by the browser .
Homology strategy refers to , agreement , domain name , If the ports are different, the same origin policy will be triggered , There's a cross domain problem .
Please note that :localhost and 127.0.0.1 Although they all point to this machine , But it's also cross domain .
Browser execution javascript Script time , Check which page the script belongs to , If it's not the same source page , Will not be implemented .
2. The emergence of cross domain in Firefox ( Here's the picture )

3. The emergence of cross domain in Google ( Here's the picture )

Two 、Vue The proxy server resolves requests across domains
Vue For cross domain solutions, click here Proxy server solves cross domain
3、 ... and 、 Tools to solve cross domain
1. Use CORS Everywhere Tools for cross domain
Tips: This tool has certain limitations , We need to use
Installation of Firefox browser CORS Everywhere, But it's super convenient !!!!
Four 、Node Solving cross domain problems
1. Use cors To solve the cross domain ( temporary 3 Methods , It will be updated )
Bear in mind : Do not install here as cros I installed it wrong at the beginning (cors and cros The two brothers look alike , It's mainly a mistake )
install :npm i cors -S
// The first cross domain approach : Import cros
var cors =require('cors')
app.use(cors())
2. Set up Access-Control-Allow-Origin To solve the cross domain ( Single interface )
// The second cross domain approach : Set the corresponding header
res.setHeader("Access-Control-Allow-Origin", "*")
3. Configure middleware to solve cross domain ( overall situation )
// The third cross domain solution
app.use((req,res,next)=>{
res.setHeader("Access-Control-Allow-Origin", "*")
next()
})
4.Node Solve cross domain complete code ( as follows )
1.Node Code
// Import express
var express =require('express')
// register app
const app =express()
// Static folder registration
// app.use(express.static("www"))
// The first cross domain approach : Import cros
// var cors =require('cors')
// app.use(cors())
// The second cross domain solution
// app.use((req,res,next)=>{
// res.setHeader("Access-Control-Allow-Origin", "*")
// next()
// })
app.get("/lo",(req,res)=>{
// The third cross domain approach : Set the corresponding header
res.setHeader("Access-Control-Allow-Origin", "*")
res.send("<h1>1231313</h1>")
})
app.get("/low",(req,res)=>{
res.send("<h1>188999798789789</h1>")
})
app.listen(3000,()=>{
console.log("Serve Is Running !!!");
})
2.Html Code for
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>Text</div>
</body>
<script>
fetch('http://localhost:3000/lo').then(res=>{
console.log(res);
})
fetch('http://localhost:3000/low').then(res=>{
console.log(res);
})
</script>
</html>
5、 ... and 、HTML Label or Jquery To solve the cross domain (jsonp Cross domain )
This cross domain writing method has been basically eliminated for a long time , It is not recommended to use , If you want to know , You can click the link below ️
Introduce jsonp Cross domain requested link
The main principle here is HTML In the tag src and href attribute , There will be no cross domain
<a></a> and <script></script>
summary
All in all : This article introduces a variety of solutions that can solve cross domain problems , The following updates will continue in React The proxy server in solves cross domain requests , Thank you for reading it patiently . I wish you all more and more skilled !!!!!!
边栏推荐
- 2022 high voltage electrician examination simulated 100 questions and simulated examination
- 数据库核心体系
- 2022 safety officer-b certificate examination simulated 100 questions and answers
- [C language] detailed explanation sequence table (seqlist)
- [one flower, one world - Professor Zheng Yi - the way of simplicity] interpretable neural network
- 正负数值的正则表达式
- 译文推荐 | 调试 BookKeeper 协议 - 无界 Ledger
- 19c SYSAUX表空间SQLOBJ$PLAN表过大,如何清理
- MySQL 8.0.30 GA
- 阿里云服务器搭建和宝塔面板连接
猜你喜欢

Get started quickly with flask (I) understand the framework flask, project structure and development environment

Detailed introduction of v-bind instruction

Activiti startup error: cannot create poolableconnectionfactory (could not create connection to database server

IJCAI 2022 | 图结构学习最新综述:研究进展与未来展望

Salted fish esp32 instance - mqtt lit LED

An entry artifact tensorflowplayground

【SwinTransformer源码阅读二】Window Attention和Shifted Window Attention部分
![Rgb-t tracking: [multimodal fusion] visible thermal UAV tracking: a large scale benchmark and new baseline](/img/9b/b8b1148406e8e521f12ddd5c12bf89.png)
Rgb-t tracking: [multimodal fusion] visible thermal UAV tracking: a large scale benchmark and new baseline

快速上手Flask(一) 认识框架Flask、项目结构、开发环境

Promise实例如何解决地狱回调
随机推荐
21天学习挑战赛-《Autosar从入门到精通-实战篇》
【一花一世界-郑一教授-繁简之道】可解释神经网络
[vscode] vscode usage
【多线程】println方法底层原理
【vscode】vscode使用
AMQ streams (1) of openshift 4 - multiple consumers receive data from partition
【多线程】long和double的非原子性协定
《PyTorch深度学习实践》第九课多分类问题(手写数字MNIST)
MySQL 8.0.30 GA
[JVM] JVM refers to floating point number
ECCV 2022 | 无需微调即可推广!基于配准的少样本异常检测框架
[C language] detailed explanation sequence table (seqlist)
19c sysaux tablespace sqlobj$plan table is too large. How to clean it up
LeetCode(剑指 Offer)- 50. 第一个只出现一次的字符
Face warp - hand tear code
正则表达式为十六进制数字?
Network engineering -- ranking of Chinese universities in Soft Science
对话MySQL之父:代码一次性完成才是优秀程序员
负数的十六进制表示
How to use gbase C API in multithreaded environment?