我正在执行注册操作,并且在后端中,当用户成功注册时,我返回他的ID,例如“ 105”,而当注册失败(用户已经存在)时,我返回“ USER_EXISTS”。我检查了邮递员的要求,回复的内容正确。
在两种情况下,我都返回“纯文本/文本”。
但是,我无法找到如何通过返回的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
}
Run Code Online (Sandbox Code Playgroud)
在RegisterService.service.ts中
addUser (registerUser) {
return this.http.post(this.apiRoot + "User/add", registerUser, httpOptions);
}
Run Code Online (Sandbox Code Playgroud)