reactjs - API登录反应未连接
问题描述
在此处输入图像描述我正在尝试登录/注册一个新用户,但是,它不会让我,我不知道如何修复它。我该怎么办?我试图找出如何做到这一点,但这对我来说有点复杂,我不知道。就像代码不想工作一样。
enter code here
import React, { useEffect, useState } from "react";
import LoginForm from "../LoginForm";
import Auth from "../../utils/Auth";
import { useLocation, useHistory } from "react-router";
//Uses the Auth methods to actually login with the LoginForm Component.
function Login() {
let location = useLocation();
let history = useHistory();
const [redirectToReferrer, setRedirectToReferrer] = useState(false);
useEffect(() => {
const { from } = location.state || { from: { pathname: "/protected" } };
if (redirectToReferrer) {
history.push(from);
}
}, [redirectToReferrer, history, location.state]);
/* We need to POST to the API the users info,
This will get passed down as a prop to the LoginForm */
const login = (data) => {
console.log("Logging in " + JSON.stringify(data));
//fetch('api/users/login', { is the error
fetch("api/users/login", {
method: "POST",
body: JSON.stringify(data),
credentials: "include",
headers: {
"Content-Type": "application/json",
},
})
.then((response) => {
if (response.status === 200) {
//All good
Auth.authenticate(() => {
//Update the boolean and take off the cuffs
setRedirectToReferrer(true);
console.log(`Response in login ${JSON.stringify(response)}`);
});
}
})
.catch((err) => {
// No beuno, kick them
console.log("Error logging in.", err);
});
};
return (
<div>
<LoginForm onLogin={login} />
</div>
);
}
export default Login;
解决方案
推荐阅读
- c - C 中的函数是否有“类型名称”?
- r - 如何修复 mtcars 数据集中的 NbClust 错误 solve.default(W)?
- ruby-on-rails - 预编译资产未正确加载
- python - 如何查看方法的文档或源代码而不是输入 Pycharm
- optimization - 如何在 CPLEX 优化工作室中求解二次目标?
- aws-lambda - 无服务器部署失败,stackTags 变量中出现 + 字符
- ruby - 输出重定向到文件
- mybatis - 如何在 MyBatis SELECT 查询中获取 java 枚举序数值
- php - 在 PHP 中使用 Curl 从网站获取请求 Cookie
- php - 为什么我的一些 shell_exec 有时会因管道损坏而失败,但仍会产生必要的输出?