[英]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.