1. JSONP
JSONP is the abbreviation of JSON with Padding. It is an unofficial protocol that allows integrating Script tags on the server side and returning them to the client side to achieve cross-domain access in the form of javascript callback (this is just a simple implementation form of JSONP).
2. Add a response header to allow cross-domain
addHeader('Access-Control-Allow-Origin:*');//Allow all origins to access
addHeader('Access-Control-Allow-Method:POST,GET');//Allow way to access
3. In the proxy mode
, the test01.html page of server A wants to access the background action of server B and returns the "test" string. At this time, a cross-domain request occurs, and an error message will appear in the browser console. Because cross-domain is a browser Caused by the same-origin policy, this problem does not exist in the server background, you can add a proxy action in server A, complete the request for action data in server B in this action, and then return to the test01.html page.
Node environment implements cross-domain requests, using the http-proxy-middleware module (ie Nodejs proxy) under the node package
http-proxy-middleware 官网:
https://github.com/chimurai/http-proxy-middleware
Official website example:
var express = require('express');var proxy = require('http-proxy-middleware');
var app = express();
app.use('/api', proxy({target: 'http://www.example.org', changeOrigin: true}));app.listen(3000);
// http://localhost:3000/api/foo/bar -> http://www.example.org/api/foo/bar