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

    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能