web服务和nginx跨域问题解决方法

1、web服务解决跨域问题(未用nginx)

在服务器页面的Response header中加入如下内容,可以实现POST跨域。
// 指定允许其他域名访问
response.addHeader("Access-Control-Allow-Origin", "*");
// 响应类型
response.addHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,PUT,DELETE,HEAD");
// 响应头设置
//header('Access-Control-Allow-Headers:x-requested-with,content-type');

2.nginx解决跨域问题

在服务器端的nginx.conf中配置增加响应头配置

location / {

  add_header Access-Control-Allow-Origin *;

  add_header Access-Control-Allow-Headers X-Requested-With;

  add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

}

这样就可以实现GET,POST,OPTIONS的跨域请求的支持,web服务器就不要第1步修改配置了

Access-Control-Allow-Origin:* 表示允许任何域名跨域访问
如果需要指定某域名才允许跨域访问,只需把Access-Control-Allow-Origin:*改为Access-Control-Allow-Origin:允许的域名
例如:header('Access-Control-Allow-Origin:http://www.redis.com.cn');

猜你喜欢

转载自blog.csdn.net/qq_16066381/article/details/81222964