//前端代码实现 let script = document.createElement(‘div‘) script.type = ‘text/javascript‘ script.src = ‘http://192.168.0.0:8080?user=shao&callback=onBack‘
function onBack(res){ console.log(JSON.stringify(res)) } //后端代码实现 onBack({‘status‘:true,user:‘admin‘})
jsonp的缺点是:只能实现get方式请求。
let xhr = new XMLHttpRequest(); 
xhr.withCredentials = true; //前端设置是否携带cookie 
//后端代码实现 
// 允许跨域访问的域名:若有端口需写全(协议+域名+端口),若没有端口末尾不用加‘/‘ 
response.setHeader("Access-Control-Allow-Origin", "http://www.domain1.com"); 
// 允许前端带认证cookie:启用此项后,上面的域名不能为‘*‘,必须指定具体的域名,否则浏览器会提示 
response.setHeader("Access-Control-Allow-Credentials", "true"); 
// 提示OPTIONS预检时,后端需要设置的两个常用自定义头 
response.setHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With");
http { # 虚拟主机配置 server { #监听80端口 listen 80; #使用example.com 访问 server_name example.com; #开启gzip压缩 gzip on; gzip_disable "MSIE [1-6]."; #默认请求 location / { root html; index index.html index.htm; } # 将http://example.com/message/下的请求转发到http://example.com:8099/message/ location /message/ { proxy_pass http://example.com:8099/message/ } # 将http://example.com/gateway/下的请求转发到http://example2.com/gateway/ location /gateway/ { proxy_pass http://example2.com/gateway/ }
}
}
... 
dev:{ 
    ... proxyTable: {
     ‘/project-manage‘: { 
        target: ‘http://192.168.2.67:7999/‘, 
        changeOrigin: true, //是否跨域 
        pathRewrite: { 
            ‘^/project-manage‘: ‘‘ 
            } 
        } 
    }, 
}
             
原文:https://www.cnblogs.com/shaofl/p/10504961.html