Mik*_*das 0 post response angular2-observables angular
我正在执行注册操作,并且在后端中,当用户成功注册时,我返回他的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)
默认情况下,如果要文本/纯文本,角度6会生成JSON响应,应将responseType设置为“ text”
addUser (registerUser) {
return this.http.post(this.apiRoot + "User/add", {responseType: 'text'});
}
Run Code Online (Sandbox Code Playgroud)
尝试将其附加在httpOptions中
检查文本/纯文本
| 归档时间: |
|
| 查看次数: |
2789 次 |
| 最近记录: |