This is the fourth day of my participation in the August More text Challenge. For details, see: August More Text Challenge
The same-origin policy
-
The same origin policy is given by the browser
-
Browsers don’t allow us to send requests to others, only to our own servers
-
When we want to send a request to someone else’s server, we are blocked by the browser
-
What is “someone else’s server”?
- If the request protocol/domain name/port number is any different, then it is someone else’s server
- At this point, the same origin policy is triggered
-
Requests that trigger the same origin policy are called cross-domain requests
Implement a cross-domain request
- Sometimes we need to implement cross-domain requests
- We need multiple servers to feed data to a single page
- So this is where we have to figure out how to solve cross-domain problems
JSONP
jsonp
It’s the way we implement cross-domain requests, it’s just the technical way we put together what we had before- Using the script tag to achieve
The nature of the script tag
-
The browser gives us a script tag
-
Its essence is to request an external resource, which is not affected by the same origin policy
-
The SRC attribute of the script tag, which is also a request, can also be received by the server
-
And:
script
Of the labelsrc
Property is a string that the browser will treat asjs
Code to execute
-
So we can use the SRC attribute of the script tag to make cross-domain requests
CORS implements cross-domain requests
<? php header("Access-Control-Allow-Origin:*"); header("Access-Control-Request-Methods:GET, POST, PUT, DELETE, OPTIONS"); header('Access-Control-Allow-Headers:x-requested-with,content-type,test-token,test-sessid'); ? >Copy the code
Configure the agent
- There are two kinds of agents, forward agents and reverse agents
Forward agent
- There is a client that needs to send a request to a non-identical server B
- We set up A server A that is the same as the client
- When the client sends A request, server A accepts it
- Server A then sends the request to server B, because the same origin policy is sent by the browser, and the servers do not have the same origin policy
- After server B receives the request, it processes the request and sends the response back to server A
- Then the server A responds to the client
- We can make cross-domain requests in this way
The reverse proxy
- Reverse proxies are generally used for load balancing
- When I ask for a server, I’m actually asking for a proxy server set up on the server side
- The proxy server distributes a number of large requests to different servers for processing
- The server sends the response to the proxy server
- The proxy server returns to the client