Angular 访问模型状态错误-Asp.Net web api标识-6-Http Post请求
我发出一个POST请求,向ASP.NET Web API Identity-Individual user Authentication Account注册一个新的用户详细信息,它工作正常,但是如果用户名已经存在,它会在其响应主体的ModelState中返回一个错误,如下所示。我想在angular中访问该错误,并将其发送到console.log()中 在角度应用程序中=>Angular 访问模型状态错误-Asp.Net web api标识-6-Http Post请求,angular,asp.net-web-api,angular6,Angular,Asp.net Web Api,Angular6,我发出一个POST请求,向ASP.NET Web API Identity-Individual user Authentication Account注册一个新的用户详细信息,它工作正常,但是如果用户名已经存在,它会在其响应主体的ModelState中返回一个错误,如下所示。我想在angular中访问该错误,并将其发送到console.log()中 在角度应用程序中=> this.http.post('http://localhost:57310/api/account/register',
this.http.post('http://localhost:57310/api/account/register', {password, confirmPassword, email})
.subscribe(data => {console.log(data);this.router.navigate(['/login'])},
(err:HttpErrorResponse)=>console.log(--?--));
我应该用什么来代替----
更新1:
我尝试了JSON.stringify(err.error.ModelState)
,但它提供了
{”“:[”名称a@a.a已被占用。”,“电子邮件”a@a.a'已被使用。“]}
但我只想
“名称a@a.a已被占用。”,“电子邮件”a@a.a“已获取。”
如果您只想获取错误消息的详细信息,请使用:
console.log(JSON.stringify(err))
查看其属性和值。如果JSON.stringify(err.error.ModelState)
提供了{”“:[“我的错误]}
然后
JSON.stringify(err.error.ModelState[“”][“0”])
提供我的错误。
它打印响应正文的消息部分,但我想打印ModelState partInfactJSON.stringify(err.error.ModelState)
提供{”“:[“我的错误]}
但我只想要我的错误部分。
console.log(JSON.stringify(err))