nginx解决前端跨域访问
本地应用:
http://localhost:63342/www/nginx/index.html
用下面语句访问远程
$.ajax({
url: 'https://www.baidu.com/img/bd_logo1.png' ,
type: 'POST',
success: function (data) {
alert('success');
},
error: function (data) {
alert('error');
}
});
报错:
XMLHttpRequest cannot load https://www.baidu.com/img/bd_logo1.png. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.
不允许跨域
安装了nginx,配置如下:
server {
listen 63343;
server_name localhost;
location /www/nginx/ {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
proxy_pass https://www.baidu.com/;
}
}
实际访问的时候,还是不能跨域!比较疑惑,请高手帮忙看看啊