nginx反向代理实现跨域请求

跨域请求可以通过JSONP实现,缺点是需要修改被请求的服务器端代码进行配合,稍显麻烦

通过在自己服务器上配置nginx的反向代理,可以轻松实现跨域请求


思路

nginx反向代理实现跨域请求

示例

服务器A中有一个页面,想请求服务器B中的api地址(http://www.b.com/api),获取JSON数据

服务器A的页面代码

<script>
$(function (){
$.get('/test.do', function (data){
alert(data);
});
});
</script>

nginx中对 /test.do 进行配置

location /test.do {
proxy_pass http://www.b.com/api;
}

相关文章:

  • 2021-11-04
  • 2021-07-16
  • 2022-12-23
  • 2021-08-31
  • 2019-12-13
  • 2021-07-31
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-04
  • 2022-12-23
  • 2021-07-28
  • 2022-12-23
  • 2022-12-23
  • 2021-07-28
相关资源
相似解决方案