简体   繁体   English

Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 (React/Redux App)

[英]Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 (React/Redux App)

I am trying to signup/login a user using JWT.我正在尝试使用 JWT 注册/登录用户。 For this I have a working API which I have created.为此,我创建了一个可用的 API。

Now, I want to use that authentication API with my React/Redux App.现在,我想在我的 React/Redux 应用程序中使用该身份验证 API。

When a user signs up I dispatch an action from my Signup component -当用户注册时,我从我的注册组件dispatch一个操作 -

const signUpHandler = (e) => {
    e.preventDefault();

    dispatch(signup(name, email, password));
  };

Now, in my userSlice I have the following reducer -现在,在我的userSlice我有以下减速器 -

signup: (state, action) => {
      fetch("http://localhost:5000/user/signup", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify(action.payload),
      })
        .then((res) => {
          return res.json();
        })
        .then((data) => {
          console.log(data);
        });
    },

After I click on Signup ie;在我点击注册后,即; after the fetch process begins, I get this error - fetch过程开始后,我收到此错误 -

Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

After digging in a little bit, I found that this error occurs when the return type from fetch is not JSON.稍微挖了一下,发现fetch的返回类型不是JSON时会出现这个错误。 But my API works prefectly fine.但我的 API 工作得很好。

Any idea what might be the issue?知道可能是什么问题吗?

Thanks in advance.提前致谢。

I just discovered the mistake and it was quite silly.我刚刚发现了这个错误,这很愚蠢。 This might prevent someone from committing the same mistake.这可能会阻止某人犯同样的错误。

When dispatching the signup/login action,在调度注册/登录操作时,

instead of dispatch(signup(name, email, password));而不是dispatch(signup(name, email, password));

Do - dispatch(signup({name, email, password}));做 - dispatch(signup({name, email, password}));

The main error was the action.payload was not what was expected(ie; an object).主要错误是action.payload不是预期的(即对象)。 As a result of which the data sent to the server was incorrect.结果发送到服务器的数据不正确。

暂无
暂无

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

相关问题 Redux 应用程序错误:未捕获(承诺中) SyntaxError:意外令牌 &lt; 在 position 0 处的 JSON - Redux app error: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 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 create-react-app Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 - create-react-app 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 - Fetch API in React for JSON file: Uncaught (in promise) SyntaxError: Unexpected token � in JSON at position 0 如何修复“Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0”错误 - How to fix “Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0” ERROR Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 使用 vuejs - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 using vuejs 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