简体   繁体   English

Fetch API in React for JSON file: Uncaught (in promise) SyntaxError: Unexpected token � in JSON at position 0

[英]Fetch API in React for JSON file: Uncaught (in promise) SyntaxError: Unexpected token � in JSON at position 0

I'm getting this error when I'm trying to parse a JSON file available on a URL.当我尝试解析 URL 上可用的 JSON 文件时出现此错误。

This is the code I'm using:这是我正在使用的代码:

let link = 'https://Company-url/file.json';
fetch(link, {
          method: "GET",
        }).then(resp => resp.json())
      .then((data) => {
        functionName.setData(data);
      });

The file is directly (publically) accessible by its URL.该文件可由其 URL 直接(公开)访问。

I read other answers and tried giving content type as JSON in the header but it is still showing the same.我阅读了其他答案并尝试在 header 中将内容类型指定为 JSON 但它仍然显示相同。

In the chrome network tab, I'm able to view the file being fetched and checked with the JSON lint, if the syntax of JSON was valid.在 chrome 网络选项卡中,如果 JSON 的语法有效,我可以查看正在使用 JSON lint 获取和检查的文件。 It was showing valid.它显示有效。

Not able to figure out what am I missing here.无法弄清楚我在这里错过了什么。

This is the JAVA code I'm using to generate the JSON file:这是我用来生成 JSON 文件的 JAVA 代码:

File jsFil = new File(i18nGuestPayResourceFolder + File.separator+outputFileName);
Writer out = new OutputStreamWriter(new FileOutputStream(jsFil), "UTF-16");
out.write(msgContainr.getMesageKeyValueJson());

Based on the comments here, I changed my JAVA code to below so that JSON is generated with UTF-8 encoding.根据此处的评论,我将 JAVA 代码更改为以下代码,以便使用 UTF-8 编码生成 JSON。

Writer out = new OutputStreamWriter(new FileOutputStream(jsFil), "UTF-16");

Now my files are being read from the API.现在正在从 API 读取我的文件。

I got to know it happens because default encoding is assumed to be UTF-8 on webpages.我知道这是因为默认编码被假定为网页上的 UTF-8 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 (React/Redux App) - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 (React/Redux App) React Js: Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 - React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 未捕获(承诺)SyntaxError:JSON 中位置 0 的意外标记 &lt; - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 未捕获(承诺)SyntaxError:意外令牌 < in JSON at position 0 来自 SpringBoot API - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 from SpringBoot API 当我尝试在我的 React 项目中获取数据时,我收到此错误: Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 - When i try to fetch data in my React project, then I get this error: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 Fetch 调用返回 react index.html 和 chrome 给出错误 Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 - Fetch call returns react index.html and chrome gives error Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 检索 JSON api 数据时出错:Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 - Error when retrieving JSON api data: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 Uncaught (in promise) SyntaxError: Unexpected token N in JSON at position 0 - Uncaught (in promise) SyntaxError: Unexpected token N in JSON at position 0 Django : Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 - Django : Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 未捕获的 Promise Rejection SyntaxError: Unexpected token u in JSON at position 0 - Uncaught Promise Rejection SyntaxError: Unexpected token u in JSON at position 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM