javawhy 2017-05-15 03:32 采纳率: 30%
浏览 4367

nginx前端跨域访问不起作用

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/;
}
}
实际访问的时候,还是不能跨域!比较疑惑,请高手帮忙看看啊

  • 写回答

3条回答 默认 最新

  • Annie_Mint 2017-05-15 06:48
    关注

    跨域在ajax页面可以用jsonp,实际实现过,可参考博客http://blog.csdn.net/Zebra_916/article/details/71743209

    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?