ajax跨域问题(五)解决'跨域'-----调用方解决跨域 - 隐藏跨域-nginx

为了试验先模拟一下

改hosts 在
C:\Windows\System32\drivers\etc
加一行 表示调用方的域名 这样a.com 就会到127.0.0.1
127.0.0.1 a.com b.com

加一配置文件 a.conf当调用方url 访问a.com会到http://localhost:8081/
但是 访问a.com/panda 会到 http://localhost:8080/test 就是服务端
D:\develop\nginx-1.15.2\conf\vhost\a.conf

server {
    listen       80;
    server_name  a.com;

    location / {
        proxy_pass http://localhost:8081/ ;
    }
    
    location /panda {
        proxy_pass http://localhost:8080/test ;
    }
}

前端请求改成↓

$.getJSON("/panda/get1").then(function(jsonObj) {
                   result = jsonObj;
               });

注意这里url用的相对地址 就是访问 a.com/panda 然后nginx的设置会使其反向代理到http://localhost:8080/test
实际上是a.com访问http://localhost:8080/test
但是 浏览器看起来是a.com访问a.com/panda根本没跨域! 当然么有跨域问题!

ajax跨域问题(五)解决'跨域'-----调用方解决跨域 - 隐藏跨域-nginx_第1张图片
image.png

↑请求头里面根本没有orgin 对浏览器来说 根本没有跨域

你可能感兴趣的:(ajax跨域问题(五)解决'跨域'-----调用方解决跨域 - 隐藏跨域-nginx)