当前位置:网站首页>Promise, async await and the solution of cross domain problems -- the principle of proxy server
Promise, async await and the solution of cross domain problems -- the principle of proxy server
2022-07-26 15:38:00 【Delete your code in the middle of the night·】
Promise In this life
1. Pure callback -- It's going to be hell , The request result appears before the callback function
2. promise For pure callback improvements : solve Back to hell ( Poor code readability ), The instructions of callback functions are more flexible
3. async await: Eliminate callback , Solve the asynchronous effect await following The content of is equivalent to .then,catch The content of is equivalent to .catch
Synchronous code solves asynchronous effect
await The back is usually promise object , Wait for the successful result ,await If not in the back promise It's equivalent to not having await
Function calls are essentially expressions ,axios The result of the function call is promise
axios It's original xhr and promise The package of
await Wait for the successful result
Cross domain
http Requests fall into two broad categories : Ordinary requests and ajax request
Normal requests do not cross domains ( The feature is to refresh the page )
ajax Cross domain ( Partial update , Do not refresh page )
What cross domain :
Violate the homology policy on the browser , It is called cross domain
Only browsers will appear cross domain
What is homology
agreement ip The three ports are called homology ( Domain name is ip And port )
If there's one different , Not homologous ( Heterologous )
How to solve ?
1、jsonp
2、cors head
3、 proxy server
1、 When we run projects npm run serve, Projects will be packaged first , Package and then run
2、 our webpack A plug-in is installed during configuration ,webpack-dev-server, It is essentially a server
After we run the project , In fact, a server is set up locally , It will process when the browser sends a request , Back to us
Packaged resources (dist)
Be careful : No, webpackdevserver When dist It can be seen in packages , After configuration
dist After packaging, it will be placed in memory , We can't see
3、 When the packaging is complete , We type in localhost:8080, enter , Actually, I sent a request to the local server , After the local server receives the request
hold dist In the middle of index.html Back to the browser , The browser will render the content , We see the page in front of us
4、 If there is a send in the page ajax request , So at this time , If we directly write the address of the target server , At this time, cross domain
Because we are currently standing at the local address and sending requests to another real server through the browser , It must violate the browser homology policy . So the browser goes to the target
The server sends ajax request , Generally, there will be cross domain .
5、 Solving cross domain problems through agents is actually a sentence , Turn the request sent by the browser to the target server into a request sent by the server to the server
To set up a proxy, you should understand the following steps :
1、 If we write code , Send the target address written directly by the request , agreement ip All ports have , Then send a request to the target server ( Cross domain )
2、 If we were writing code , There is no protocol written to the destination address of the send request 、ip And port , Only the path is written , It is sent locally by default , No cross domain
After the first two steps , We are transforming the request sent by the browser to the target server into the request sent by the browser to the local server
3、 By configuring the agent of the local development server , To solve the cross domain
In the installation webpack-dev-server When , This server will automatically install a middleware ,http-proxy-middleware
This middleware has the function of forwarding requests
The essence of configuring agent is to configure this middleware , This middleware does not match , Then it's equivalent to useless , Configured with , The middleware takes effect ( watchdog )
The request has been transferred from the target server to the local server , We just need to tell the watchdog , What needs to be forwarded
Then we only need to send local requests later , The watchdog went to check , If you detect a request that needs to be forwarded , Will forward the request
Vue Configuration agent of
devServer:{
// watchdog
proxy: {
"/api": {
// /api Represents the forwarding ID , Essence is the beginning of the path
// This sign may exist in its own path , It may also be added by yourself
// As long as the request is sent locally , Check whether the path starts with the forwarding ID , If so, forward
target: "http://localhost:3000",
// The goal is , Represents the address of the target server to be forwarded , There is no path , Only agreement IP port
// The path to the local request already haspathRewrite: {"^/api" : ""}
// The path to rewrite , It depends on whether there is identification in the path of the final target server , If there was this logo
// Then there is no need to rewrite , If , No, I added it myself , Then rewrite this logo
}
}
}
边栏推荐
- 【留点代码】将transformer运用到目标检测上来,通过debug了解模型的模型运算流程
- QT is the most basic layout, creating a window interface
- 信用卡数字识别(opencv,代码分析)
- MYSQL 命令大全
- R language ggplot2 visualization: use the ggballoonplot function of ggpubr package to visualize the balloon graph (visualize the contingency table composed of two classification variables), and config
- Sword finger offer II 009. subarray with product less than k
- 什么是传输层协议TCP/UDP???
- 持续集成(二)Jenkins基本使用介绍
- 全志A40i工业核心板,100%国产4核ARM Cortex-A7,支持“双屏异显”【显示接口能力,工业HMI首选方案】
- R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、并根据模型系数写出回归方程、使用fitted函数计算出模型的拟合的y值(响应值)向量
猜你喜欢

If you want to be good at work, you must first use its tools -c language expansion -- embedded C language (11)

FOC motor control foundation
![[basic] the difference between dynamic link library and static link library](/img/d5/fe7880e3fa91faff10a1c31870cce0.png)
[basic] the difference between dynamic link library and static link library

数仓:爱奇艺数仓平台建设实践

信用卡数字识别(opencv,代码分析)

C # set different text watermarks for each page of word

sklearn clustering聚类

【留点代码】将transformer运用到目标检测上来,通过debug了解模型的模型运算流程

# 工欲善其事必先利其器-C语言拓展--嵌入式C语言(十一)

教程篇(7.0) 05. 通过FortiClient EMS发放FortiClient * FortiClient EMS * Fortinet 网络安全专家 NSE 5
随机推荐
Remote desktop on Jetson nano
Credit card number recognition (openCV, code analysis)
University rankings in Beijing
Enterprise digital transformation needs in-depth research, and it cannot be transformed for the sake of transformation
如何将规划图转成带经纬度的矢量数据geojson
Creation and traversal of binary tree
Pytorch installation CUDA corresponding
FOC电机控制基础
Familiarize you with the "phone book" of cloud network: DNS
全志A40i工业核心板,100%国产4核ARM Cortex-A7,支持“双屏异显”【显示接口能力,工业HMI首选方案】
Pytorch--- advanced chapter (function usage skills / precautions)
什么是虚拟摄像头
Super simple! It only takes a few steps to customize the weather assistant for TA!!
OSPF综合实验
QT is the most basic layout, creating a window interface
超简单!只需简单几步即可为TA定制天气小助理!!
Google tries to introduce password strength indicator for chromeos to improve online security
Sword finger offer II 009. subarray with product less than k
No module named ‘win32gui‘
北京的大学排名