相关疑难解决方法(0)

Angular6-读取文本/纯文本的响应主体

我正在执行注册操作,并且在后端中,当用户成功注册时,我返回他的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)

post response angular2-observables angular

0
推荐指数
1
解决办法
2789
查看次数

标签 统计

angular ×1

angular2-observables ×1

post ×1

response ×1