当前位置:网站首页>通俗讲跨域
通俗讲跨域
2022-06-25 15:32:00 【竹林小虾】
前端开发前后端分离的项目,一定会经常遇到跨域(Cross-Origin)的问题,为什么会有跨域?如何解决跨域呢?以及最常见的方法有哪些呢?
what
跨域的全称是跨域资源共享
场景:当你请求的接口地址和现在前端文件服务所在的域(请求的协议,ip和端口)不同的时候,就会报错,如下图
why
跨域问题的产生其实是浏览器的同源策略造成的,同源策略是浏览器的保护机制,只允许网页请求同一域名下的服务。
同源策略的要求是3同:协议,域名和端口都要保持一致
how
【配置后端】CORS
浏览器是否启用跨域保护机制根据后端配置来响应的,所以服务器端需要做一个允许跨域的配置即可
如何配置:
修改接口的响应头:Access-Control-Allow-Origin,值为*,,浏览器判断通过就会允许跨域
【配置中间服务器】反向代理
意思是用一个代理服务器来代理真正的服务端发送请求
【配置中间服务器】正向代理
意思是用一个代理服务器来代理客户端发送请求,一般用浏览器做代理服务器,这个直接在webpack里面配置
JSONP【不推荐】
其实是通过script标签的特性(无跨域限制)来实现跨域的,也需要服务器端配合,且只能用get方法, 有安全隐患,不推荐
边栏推荐
- Statistical analysis - data level description of descriptive statistics
- Go build reports an error missing go sum entry for module providing package ... to add:
- Core mode and immediate rendering mode of OpenGL
- Custom structure type
- Is it safe to open a stock account in Guoxin golden sun?
- System Verilog - thread
- MySQL field truncation principle and source code analysis
- Completabilefuture of asynchronous tools for concurrent programming
- MySQL performance optimization - index optimization
- Leetcode123 timing of buying and selling stocks III
猜你喜欢

中国高校首次!全球唯一!同济学子斩获国际大奖

JVM memory region details

If a thread overflows heap memory or stack memory, will other threads continue to work
![[paper notes] poly yolo: higher speed, more precise detection and instance segmentation for yolov3](/img/28/6d58759a4a4b18923a5ed5ed573956.jpg)
[paper notes] poly yolo: higher speed, more precise detection and instance segmentation for yolov3

Introduction to flexible array

Agent and classloader

Distributed token

04. binary tree

Generic - learning notes

Yolov5 Lite: fewer parameters, higher accuracy and faster detection speed
随机推荐
(1) Introduction
Differences and solutions of redis cache avalanche, cache penetration and cache breakdown
[paper notes] poly yolo: higher speed, more precise detection and instance segmentation for yolov3
Leetcode121 timing of buying and selling stocks
How GC determines whether an object can be recycled
JVM memory region details
[paper notes] street view change detection with deconvolutional networks
Is Guoxin golden sun reliable? Is it legal? Is it safe to open a stock account?
QT animation loading and closing window
MySQL performance optimization - index optimization
AB string interchange
JSON module dictionary and string conversion
剑指 Offer II 091. 粉刷房子
Go build reports an error missing go sum entry for module providing package ... to add:
Simulating Sir disease transmission model with netlogo
Single user mode
If a thread overflows heap memory or stack memory, will other threads continue to work
股票开户用什么app最安全?知道的给说一下吧
Completabilefuture of asynchronous tools for concurrent programming
Agent and classloader