【发布时间】:2023-01-28 21:08:31
【问题描述】:
我正在做一个获取反应并得到这些错误,我无法弄清楚如何解决它。我正在使用 TypeScript 和 C# 休息服务。它在邮递员中运行良好,但在客户端中存在这些问题。
我试过禁用所有浏览器扩展,也尝试过其他浏览器。这没有用。 我期待从 REST 调用收到状态“201”。
单击按钮
<Button className="w-100 btn btn-lg btn-primary" type='submit'onClick={e => {e.preventDefault() handleForm()}}>Register</Button>
javascript: ` 异步函数 handleForm() { console.log(JSON.stringify({ ...注册}))
const endpoint = 'http://localhost:44309/api/Users/Register';
const data = {
email: registration.email,
userName: registration.username,
password: registration.password,
confirmPassword: registration.passwordConfirmation,
userTypeId: 4
};
// Default options are marked with *
const response = await fetch(endpoint,
{
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json'
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
return response.json();
}`
这是 C# 的休息方法: ` [HttpPost([动作]")] public IActionResult Register([FromBody] ApplicationUser applicationUser) { var userExists = _dbContext.AppUsers.FirstOrDefault(u => u.Email == applicationUser.Email); //todo: 添加验证码
if (userExists != null)
{
return BadRequest("User with the same email address already exists");
}
applicationUser.Password = HashService.HashPassword(applicationUser.Password);
#if (!DEBUG)
applicationUser.ConfirmPassword = "True";
#endif
_dbContext.AppUsers.Add(applicationUser);
_dbContext.SaveChanges();
return StatusCode(StatusCodes.Status201Created);
}`
【问题讨论】:
标签: javascript reactjs rest fetch