Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据

生活中,最使人疲惫的往往不是道路的遥远,而是心中的郁闷;最使人痛苦的往往不是生活的不幸,而是希望的破灭;最使人颓废的往往不是前途的坎坷,而是自信的丧失;最使人绝望的往往不是挫折的打击,而是心灵的死亡。所以我们要有自己的梦想,让梦想的星光指引着我们走出落漠,走出惆怅,带着我们走进自己的理想。

导读:本篇文章讲解 Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

场景

网络接口返回json格式数据,可以直接在浏览器中访问。

Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据

可以在页面上右键另存为json格式文件

 Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据

怎样在本地使用nginx配置,模拟get接口返回该json文件,使前端使用ajax请求数据时,可以返回该json数据,

并且浏览器还不提示跨域。

注:

博客:
BADAO_LIUMANG_QIZHI的博客_霸道流氓气质_CSDN博客-C#,SpringBoot,架构之路领域博主
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

1、下载nginx

nginx: download

下载对应的版本并解压,找到conf下的配置文件

Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据

2、修改配置文件

修改如下几个地方

Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据

修改默认类型为json

http {
    include       mime.types;
    default_type  application/json;

配置别名访问

        location /json/ {
   default_type application/json;
   alias D:/jsonData/;
        }

这个配置的意思是先设置响应的内容格式为application/json;

这里的alias配置方式是请求替代的方式,即/json/的请求就去对应D:/jsonData/目录下去找。

配置跨域问题

  add_header Access-Control-Allow-Origin *;
  add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
  add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
 
  if ($request_method = 'OPTIONS') {
   return 204;
  }

3、完整示例nginx配置

​
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/json;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
  add_header Access-Control-Allow-Origin *;
  add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
  add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
 
  if ($request_method = 'OPTIONS') {
   return 204;
  }
        listen       3000;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /json/ {
   default_type application/json;
   alias D:/jsonData/;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

​

4、启动nginx,浏览器中访问

http://localhost:3000/json/migration.json

Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据

5、前端jquery发起ajax请求示例

        $.ajax({
            url: 'http://localhost:3000/json/migration.json',
            type: "get",
            dataType: 'json',
            success: function (data) {

            }
        });

6、前端请求效果

Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/136117.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!