zoukankan      html  css  js  c++  java
  • nginx解决Ajax跨域问题

    今天遇到一个ajax跨域问题,下拉框的数据源要从一个接口获得,但是该接口被部署到另外一台服务器上,在本地可以通过http请求访问,并可以返回json的数据,但是放到页面中不可以获取到下拉框的值,发现chrome控制台中该请求成功,但是没有返回值,于是便遇到了跨域的问题,请教一同事,问题得到解决:

    1.搭建nginx服务器

      下载nginx,我用的是nginx1.0.0,下载之后放到一个目录中,修改其中的配置文件conf目录中的ngnix.conf文件

    2.找到配置中的server{}标签,在里面的localtion/{}标签中添加一句

      proxy_pass http://localhost:8080/;

      添加后的整体效果为:

    1 location / {
    2             root   html;
    3             index  index.html index.htm;
    4             proxy_pass     http://localhost:8080/;
    5         }    

      其中第4行为新加入的

    3.在该标签下自己新建一个标签,如下:

    1 location /partner{
    2             proxy_pass http://10.23.3.31/partner;
    3         }

    这个proxy_pass http://10.23.3.31/partner就是你要访问的域

    server标签的整体配置为:

     1 server {
     2         listen       80;
     3         server_name  localhost;
     4 
     5         #charset koi8-r;
     6 
     7         #access_log  logs/host.access.log  main;
     8 
     9         location / {
    10             root   html;
    11             index  index.html index.htm;
    12             proxy_pass http://localhost:8080/;
    13         }
    14         location /partner{
    15             proxy_pass http://10.23.3.31/partner;
    16         }
    17 
    18         #error_page  404              /404.html;
    19 
    20         # redirect server error pages to the static page /50x.html
    21         #
    22         error_page   500 502 503 504  /50x.html;
    23         location = /50x.html {
    24             root   html;
    25         }
    26 
    27         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    28         #
    29         #location ~ \.php$ {
    30         #    proxy_pass   http://127.0.0.1;
    31         #}
    32 
    33         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    34         #
    35         #location ~ \.php$ {
    36         #    root           html;
    37         #    fastcgi_pass   127.0.0.1:9000;
    38         #    fastcgi_index  index.php;
    39         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    40         #    include        fastcgi_params;
    41         #}
    42 
    43         # deny access to .htaccess files, if Apache's document root
    44         # concurs with nginx's one
    45         #
    46         #location ~ /\.ht {
    47         #    deny  all;
    48         #}
    49     }

    其中12、14、15、16行为自己手动添加的

    修改完毕后,启动nginx.exe程序

    注意,此时访问的路径会发生变化,不需要带端口号,如果之前的访问为:localhost:8080//oss-api-server...

    现在需要改为:localhost//oss-api-server...即可得到从别的域中取回来的数据!

  • 相关阅读:
    iOS-实现键盘右上角完成按钮
    iOS-开发中单例模式的实现
    iOS-实现高斯模糊效果(swift)
    iOS-解决UITableView有footerView时最后一个cell不显示分割线问题
    fenics 笔记 -- Possion Problem
    笔记
    Hyper-reduced projective dynamics 手推公式
    Gmsh 四面体单元剖分
    SoftRoboSim 之程序框架
    物理引擎中的时间积分方法及求解
  • 原文地址:https://www.cnblogs.com/ylee/p/3047371.html
Copyright © 2011-2022 走看看