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)
1. The difference between Http and Https HTTP: It...
This article shares the specific code of making a...
This article shares the specific code of jQuery t...
Custom tags can be used freely in XML files and HT...
How to use the concat function in MySQL: CONCAT(s...
This article example shares the application code ...
There was a shaking barrage on TikTok a while ago...
Table of contents 1. Constructors and instances 2...
The essence of a flat website structure is simpli...
I struggled with a problem for a long time and re...
<br />For each of our topics, the team will ...
Preface This experiment prepares two virtual mach...
C++ connects to MySQL for your reference. The spe...
<br />The page uses UTF8 encoding, and the h...
Use HSSFWorkbook in Apache.POI to export to Excel...