1. Set CORS response header to achieve cross-domainCross-Origin Resource Sharing (CORS) 1.1 What is CORSCORS (Cross-Origin Resource Sharing), cross-origin resource sharing. CORS is the official cross-domain solution. Its feature is that it does not require any special operations on the client and is completely processed on the server. It supports get and post requests. The Cross-Origin Resource Sharing standard adds a set of HTTP header fields that allow servers to declare which origins have access to which resources through browsers. 1.2 How does CORS work?CORS sets a response header to tell the browser that the request is allowed to cross domains. After receiving the response, the browser will release the response. 1.3 What is CORS used for?ajaxDemo.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>CORS</title> <style> #result { width: 200px; height: 100px; border: solid 1px #90b; } </style> </head> <body> <button>Send Request</button> <div id="result"></div> <script> const btn = document.querySelector('button'); btn.onclick = function () { //1. Create object const x = new XMLHttpRequest(); //2. Initialization settings x.open("GET", "http://127.0.0.1:8080/cors-server"); //3. Send x.send(); //4. Bind event x.onreadystatechange = function () { if (x.readyState === 4) { if (x.status >= 200 && x.status < 300) { document.getElementById('result').innerText = x.response; } } } } </script> </body> </html> server.js //1. Import express const express = require('express'); //2. Create application object const app = express(); //3. Create routing rules // request is the encapsulation of the request message // response is the encapsulation of the response message app.all('/cors-server', (request, response)=>{ //Set the response header response.setHeader("Access-Control-Allow-Origin", "*"); // Allow all cross-domain requests* // response.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:5500"); // Allow specified requests to cross domains // response.setHeader("Access-Control-Allow-Headers", '*'); // Allow custom request header tags // response.setHeader("Access-Control-Allow-Method", '*'); // Allow all requests to cross domains * // response.setHeader("Access-Control-Allow-Method", 'get'); // Allow get requests to cross domain response.send('hello CORS'); }); //4. Listening port to start service app.listen(8080, () => { console.log("The service has been started, port 8080 is listening...."); });
This is the end of this article about Ajax cross-domain solution and setting CORS response header to achieve cross-domain case details. For more relevant Ajax cross-domain solution content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Implementation of docker redis5.0 cluster cluster construction
>>: Migrate virtual machines between VMware Workstation and vSphere (picture and text)
background A new server was added in the company,...
Table of contents 1. Install and import 2. Define...
1. Introduction to KVM The abbreviation of kernel...
1 Pull the image from hup docker pull nginx 2 Cre...
<br />Use of line break tag<br>The lin...
Here is a text hovering and jumping effect implem...
1. Background We do some internal training from t...
1. The relationship between fonts and character d...
The principle of uploading pictures on the front ...
Copy code The code is as follows: Difference betw...
Why do I want to organize the content in this area...
To install Jenkins on CentOS 8, you need to use t...
When key is not added to the v-for tag. <!DOCT...
Only display Docker container mount directory inf...
Table of contents Introduction to the Decorator P...