比较来自世界各地的卖家的域名和 IT 服务价格

过程 400 糟糕请求OT。 WebApi 在 Angular 6 /使用 HttpClient/

以下是核 Asp.net WebAPI, 返回一个错误请求,详细错误信息有关其参数的详细信息,例如,当用户副本试图注册。


public async Task<iactionresult> Register/[FromBody] RegisterModel registerModel/
{
if /ModelState.IsValid/
{
var user = new ApplicationUser
{
//TODO: Use Automapper instead of manual binding

UserName = registerModel.Username,
FirstName = registerModel.FirstName,
LastName = registerModel.LastName,
Email = registerModel.Email
};
var identityResult = await this.userManager.CreateAsync/user, registerModel.Password/;
if /identityResult.Succeeded/
{
await signInManager.SignInAsync/user, isPersistent: false/;
return Ok/GetToken/user//;
}
else
{
Console.WriteLine/"Errors are : "+ identityResult.Errors/;
return BadRequest/identityResult.Errors/;
}
}
return BadRequest/ModelState/;


答案在侧面处理 Angular 通过以下方式:

user.service.ts


register/user: User/ {
// let headers = new Headers/{ 'Content-Type': 'application/json' }/;
//var reqHeader = new HttpHeaders/{ 'Content-Type': 'application/json'}/;
const reqHeader = new HttpHeaders//.set/'Content-Type', 'application/json'/
.set/'Accept', 'application/json'/;
//return this.http.post/this.rootUrl + '/api/account/register', body,{headers : reqHeader}/;

return this.http.post/this.apiUrl+ '/api/account/register', user,{headers : reqHeader}/;
}


以上方法被调用:

register.component.ts


this.userService.register/this.registerForm.value/
.pipe/map//res: Response/ =&gt; res.json////
.subscribe/
data =&gt; {
this.alertService.success/'Registration successful', true/;
this.router.navigate/['/login']/;
},
/error:HttpErrorResponse/ =&gt; {
// let validationErrorDictionary = JSON.parse/error.text///;
// for /var fieldName in validationErrorDictionary/ {
// if /validationErrorDictionary.hasOwnProperty/fieldName// {
// this.errors.push/validationErrorDictionary[fieldName]/;
// }
// }
// this.alertService.errorMsg/this.errors/;
console.log/error.error/;
}/;


当我试图做的时候 Postman, 我得到了完美的结果,如下所示 :

Postman 结果:

但结果是相同的,尽管尝试了几个代码片段,不起作用,以及所有这些寄存器 'Bad result' 作为一个答案。

Angular 结果:

我真的注意到了,虽然答案位于网络选项卡上 ... 只是缺乏想法来处理它。

错误描述:


这里缺少什么? 你的答案将非常感激 !
</iactionresult>
已邀请:

三叔

赞同来自:

我也遇到了同样的问题 angular 7. 但是,我今天使用下面的代码更正了它。 :

在运营中 :


registerUser/userdata/: Observable<any> {
return this.httpClient.post/'url', userdata/.pipe/catchError/this.handleError//;
}

handleError/error: HttpErrorResponse/ {
return throwError/error/;
}


在 register.component.ts 年 :


postData// {
this.auth.registerUser/userdata/.subscribe/
/resp/ =&gt; {
console.log/resp/;
},
/error/ =&gt; {
console.log/error.error/;
}
/;
}


</any>

知食

赞同来自:

回复识别错误 Dotnet Core 400 它是一个数组{代码,描述}。
所以要处理它 typescript, 首先定义接口/事实上,它不是必需的,但严格检查。


interface ClientError {
code: string;
description: string;
}


然后在处理错误的部分中,请按照下列步骤操作,


/error:HttpErrorResponse/ => { 
if/error.status===400/{
const errors: Array<clienterror> = error.error;
errors.forEach/clientError =&gt; {
console.log/clientError.code/;
}/;
}
}


更新:它是特定的 Identity Framework. /登录和注册/. 在其他领域,错误处理将需要手工制作来匹配此模式。
</clienterror>

龙天

赞同来自:

你得到 400 来自您的HTTP状态代码 API 在 angular 附录 AND 在 postman, 和:
https://i.stack.imgur.com/DYAhE.png
BTW

: 400 状态是 API 预期行为。 他无法创建用户并发送错误描述以及相应的 HTTP-status /400/。如果你想应对这个错误 API, 最好的选择将是类似的

/referenced angular
https://angular.io/api/common/ ... ponse
type

/ :


this.userService.register/this.registerForm.value/
.pipe/map//res: Response/ => res.json////
.subscribe/
data => {
this.alertService.success/'Registration successful', true/;
this.router.navigate/['/login']/;
},
/error:HttpErrorResponse/ => {
let errorPayload = JSON.parse/error.message/;
//ToDo: apply your handling logic e.g.:
//console.log/errorPayload[0].description
console.log/error.error/;
}/;

要回复问题请先登录注册