我正在执行注册操作,当用户成功注册时,我会在后端返回他的 ID,例如“105”,当注册失败(用户已存在)时,我返回“USER_EXISTS”。我已经在 Postman 上检查了请求,响应正文是正确的。
在这两种情况下,我都会返回“纯文本/文本”。
但是,我无法找到如何通过返回的 Observable 对象获取响应正文。
在register.component.ts中:
userExists = "";
onSubmit() {
const newUser : RegisterUser = { email: this.register.email,
password: this.register.password,
firstName: this.register.firstName,
lastName: this.register.lastName,
phoneNumber: this.register.phoneNumber}
this.registerService.addUser(newUser)
.subscribe(response => (this.userExists = response)); //The purpose of this line is to get the body of the text/plain response and assign it into the this.userExists string
}
在RegisterService.service.ts中
addUser (registerUser) {
return this.http.post(this.apiRoot + "User/add", registerUser, httpOptions);
}
默认情况下,角度 6 产生JSON回应
如果你想要文本/纯文本你应该设置响应类型 as 'text'
addUser (registerUser) {
return this.http.post(this.apiRoot + "User/add", {responseType: 'text'});
}
尝试将其附加到http选项
核实文本/纯文本 https://stackblitz.com/edit/angular-rh7r9f?file=app%2Fapp.module.ts
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)