如何修复“未捕获承诺SyntaxError:JSON中位置0处的意外令牌<”错误

如何解决如何修复“未捕获承诺SyntaxError:JSON中位置0处的意外令牌<”错误

经过数小时的网上搜索,我仍未找到解决问题的方法。所以我正在React和Java Spring Boot上构建一个全栈CRUD应用程序。到目前为止,我已经能够在localhost:// 8080上运行后端soring引导。当我在localhost:// 3000上运行前端React时,会出现问题-> React App保持loading并且我看到“未捕获(承诺)SyntaxError:意外令牌控制台中的错误

App.js:

import React,{ Component } from 'react';
import logo from './logo.svg';
import './App.css';

class App extends Component {
state = {
isLoading: true,groups: []
};

async componentDidMount() {
const response = await fetch('/api/groups');
const body = await response.json();
this.setState({ groups: body,isLoading: false });
} ->

在此处获取语法错误

render() {
const {groups,isLoading} = this.state;

if (isLoading) {
  return <p>Loading...</p>;
}

return (
  <div className="App">
    <header className="App-header">
      <img src={logo} className="App-logo" alt="logo" />
      <div className="App-intro">
        <h2>JUG List</h2>
        {groups.map(group =>
          <div key={group.id}>
            {group.name}
          </div>
        )}
      </div>
    </header>
  </div>
);
}
}

export default App;

package.json:

{
"name": "app","version": "0.1.0","private": true,"dependencies": {
"@testing-library/jest-dom": "^4.2.4","@testing-library/react": "^9.3.2","@testing-library/user-event": "^7.1.2","bootstrap": "4.1.3","react": "^16.14.0","react-cookie": "3.0.4","react-dom": "^16.14.0","react-router-dom": "4.3.1","react-scripts": "3.4.3","reactstrap": "6.5.0"
},"scripts": {
"start": "react-scripts start","build": "react-scripts build","test": "react-scripts test","eject": "react-scripts eject","proxy": "http://localhost:8080" //====>PROBLEMATIC CODE(should be outside 
 //the script block)
},"eslintConfig": {
"extends": "react-app"
},"browserslist": {
"production": [
  ">0.2%","not dead","not op_mini all"
],"development": [
  "last 1 chrome version","last 1 firefox version","last 1 safari version"
 ]
}

response.json:

{
"$id": "response.json#","$schema": "http://json-schema.org/draft-06/schema#","type": "object","required": [
"status","statusText","httpVersion","cookies","headers","content","redirectURL","headeRSSize","bodySize"
],"properties": {
"status": {
  "type": "integer"
},"statusText": {
  "type": "string"
},"httpVersion": {
  "type": "string"
},"cookies": {
  "type": "array","items": {
    "$ref": "cookie.json#"
  }
},"headers": {
  "type": "array","items": {
    "$ref": "header.json#"
  }
},"content": {
  "$ref": "content.json#"
},"redirectURL": {
  "type": "string"
},"headeRSSize": {
  "type": "integer"
},"bodySize": {
  "type": "integer"
},"comment": {
  "type": "string"
}
}
}

解决方案:

"scripts": {
"start": "react-scripts start","eject": "react-scripts eject"
},"proxy": "http://localhost:8080"

GitHub Repo

解决方法

/api/groups调用返回的响应是什么?根据错误消息,响应可能是HTML格式。但是它被解析为JSON(在response.json()中)调用。

尝试将您的代码临时更改为以下内容:

const body = await response.text();
console.log(body);

或使用“网络”标签检查服务器的响应。

,
"proxy": "http://localhost:8080" 

上一行应位于下面的“脚本”块之外。因为“代理”不是脚本的一部分,而是一个与React Server see details对话的端口,就像SO:

"scripts": {
"start": "react-scripts start","build": "react-scripts build","test": "react-scripts test","eject": "react-scripts eject"
},"proxy": "http://localhost:8080"

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

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?