提取JSON会返回错误未捕获(在承诺中)SyntaxError:意外令牌<在JSON中的位置0和状态代码304:未修改 [英] Fetching JSON returns error Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 and status code 304: Not Modified

查看:234
本文介绍了提取JSON会返回错误未捕获(在承诺中)SyntaxError:意外令牌<在JSON中的位置0和状态代码304:未修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取JSON文件,但它返回错误未捕获(已承诺)SyntaxError:意外令牌<在JSON中位于位置0 304:未修改.我尝试添加标头'Content-Type': 'application/json'Accept': 'application/json',但随后返回错误 404:找不到.

I am trying to fetch a JSON file but it returns the errors Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 and 304: Not Modified. I tried to add headers 'Content-Type': 'application/json and 'Accept': 'application/json' but then it returns error 404: Not found.

这是我获取JSON的代码:

Here is the code where I am fetching my JSON:

import React from 'react';

export default class LabExperiment extends React.Component {

componentWillMount() {
    const readmePath = require("./docs/iat/iatDependency.json");

    fetch("./docs/iat/iatDependency.json")
    .then(response => {
        response.json();
    })
    .then(text => {
        console.log(text);
    });
}

render() {
    return(
        <div>Hello</div>
    )
  }
}

这是我的json文件:

Here is my json file:

{
    "js": [
        "../jsPsych/jspsych.js",
        "../jsPsych/plugins/jspsych-iat-image.js",
        "../jsPsych/plugins/jspsych-iat-html.js",
        "../jsPsych/plugins/jspsych-html-keyboard-response.js"
    ],
    "css": [
        "../jsPsych/css/jspsych.css"
    ]
}

我也曾尝试使用response.text()而不是json(),但是它返回了我的index.html文件.

I also at one point tried to use response.text() instead of json() but it returned my index.html file.

过去一周来,我一直在网上寻找解决方案,但对他人有效的方法对我而言无效.有什么建议吗?

I have been looking around the web for the past week trying to find a solution but what worked for others didn't work for me. Any advice?

当我执行console.log(response.headers.get('Content-Type'))时,它返回了text/html; charset=UTF-8.它不应该已经是application/json了吗?为什么初始编码为"text/html"?

When I did console.log(response.headers.get('Content-Type')) it returned text/html; charset=UTF-8. Shouldn't it already be application/json? Why is the initial encoding 'text/html'?

推荐答案

我认为您可能正在使用create-react-app.与create-react-app中一样,webpack-dev-server用于处理请求,并且对于每个请求,它都为index.html服务.所以你得到

I think you might be using create-react-app. As in create-react-app, webpack-dev-server is used to handle the request and for every request it serves the index.html. So you are getting

意外令牌< JSON中位置0和状态代码304:未修改

Unexpected token < in JSON at position 0 and status code 304: Not Modified

要解决此问题,您可以执行以下操作:

So to solve this, you can do following:

  1. 运行npm run eject

之后,在应用程序的根目录下创建了config文件夹.

After that config folder is created at the root of the app.

  1. 转到config/webpackDevServer.config.js.

然后我们需要在webpackDevServer.config.js中设置disableDotRule: false.

Then we need to set disableDotRule: false in webpackDevServer.config.js.

    historyApiFallback: {
     // previously disableDotRule: true,
     disableDotRule: false,
    },

  • 将您的json文件保存在public文件夹中,即docs/iat/iatDependency.json.

  • Keep your json file i.e docs/iat/iatDependency.json in public folder.

    使用fetch('/docs/iat/iatDependency.json').then(res => res.json()).then(data => console.log('data', data))

    这篇关于提取JSON会返回错误未捕获(在承诺中)SyntaxError:意外令牌&lt;在JSON中的位置0和状态代码304:未修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆