ajax完美解决跨域问题(jsonp、nginx反向代理)

做过web前端人都知道,经常会有ajax跨域问题,下面列举我经常使用的解决办法
第一种:使用jsonp,jQuery的ajax方法支持jsonp,但是最大的缺点就是只支持get方式,而且服务端也要修改
客户端 test.html代码

  
  
  
    工作端  
      
      
      
      
  
  
  
  

第二种:nginx反向代理,我的nginx版本nginx-1.10.0
首先在 conf\apiserver-reverse-proxy-conf\bingli\main.conf ,没有相关目录和文件就新

location ~* ^/uc/.*{  
    proxy_set_header Host $host;  
    proxy_set_header X-Real-Ip $remote_addr;  
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
    proxy_pass http://192.168.10.111:8080;  
}

然后在nginx主配置文件添加加粗内容,即把代理文件加载进来

location / {  
            root   html;  
            index  index.html index.htm;  
        }  
include apiserver-reverse-proxy-conf/bingli/main.conf;  

重启nginx,之后ajax发请求到
http://localhost/uc/aa
http://localhost/uc/bb?token=xxxx
都会被转发到
http://192.168.10.111:8080/uc/aa
http://192.168.10.111:8080/uc/bb?token=xxxx

你可能感兴趣的:(ajax完美解决跨域问题(jsonp、nginx反向代理))