首页 > 解决方案 > ReactJS Fetch 无法加载 json,输入意外结束

问题描述

我正在从 nginx 服务器加载一个 json 文件。使用 reactjs 公共数据加载工作正常。

但我只是将数据文件夹移动到使用 nginx 的不同服务器

fetch(cdnUrl + "/data/content.json", { mode: 'no-cors' })
            .then(res => res.json())
            .then(result => {
                console.log('result is');
                console.log(result);
               
            }).catch(error => {
console.log(error);
            });

我想我一定是错误地配置了 nginx。但我不知道到底是什么问题

配置

server {
        listen          80;
        listen          [::]:80;
        server_name     cdn.server;

        location / {
                root /home/ec2-user/cdn;
                index   index.html;
                log_not_found   off;
                access_log      off;
        }

# gzip
        gzip            on;
        gzip_vary       on;
        gzip_proxied    any;
        gzip_comp_level 6;
        gzip_types      text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;

}

我收到以下错误 错误 SyntaxError: Unexpected end of input

但在我将 json 移动到 nginx 之前它工作正常(同一台服务器运行良好)

响应头

Content-Encoding: gzip
Content-Type: application/json
Date: Fri, 09 Oct 2020 08:11:42 GMT
ETag: W/"5f79f20f-548"
Last-Modified: Sun, 04 Oct 2020 16:02:23 GMT
Server: nginx
Vary: Accept-Encoding

标签: reactjsnginxnginx-config

解决方案


推荐阅读