React + .NET Core Web API,获取失败

React + .NET Core Web API, Failed to fetch

我正在尝试使用 React 登录、注册页面,并且我正在使用 JWT 进行身份验证。 我的后端工作正常,我已经在 Postman 上测试过它并且可以工作。

但是当我开始构建前端时出现错误。

我的代码是:

 import { METHODS } from "http";
 import React, { SyntheticEvent, useState } from "react";
 const Register = () => {
 const [FirstName,setName ] = useState('');
 const [LastName,setLastName ] = useState('');
 const [Email,setEmail ] = useState('');
 const [Password,setPassword ] = useState('');
 const submit  = async (e:SyntheticEvent) => {
 e.preventDefault();

 const response = await fetch ('http://localhost:44385/api/auth/register' , {
 method:'POST',
 headers: {'Content-Type' : 'application/json'},
 body: JSON.stringify( {
    FirstName,
    LastName,
    Email,
    Password
  })
})
 const content = await response.json();
console.log(content);

}
return (
<form onSubmit={submit}>

<h1 className="h3 mb-3 fw-normal">Register</h1>

<input  className="form-control"  placeholder="FirstName" required
onChange={e=> setName(e.target.value)} />

<input  className="form-control"  placeholder="Last Name" required
onChange={e=> setLastName(e.target.value)} />

<input type="email" className="form-control"  
  placeholder="name@example.com" required 
 onChange={e=> setEmail(e.target.value)}/>

<input type="password" className="form-control"  placeholder="******" 
  required 
onChange={e=>setPassword(e.target.value)}/>

 <button className="w-100 btn btn-lg btn-primary" 
   type="submit">Submit</button>
 </form>
);
};

 export default Register;

错误:

尝试添加:

 "proxy": "http://localhost:44385",

你的 React 项目的 package.json

我还建议将您的抓取放在 try/catch 块中。