使用服务文件时,我没有得到实际响应

时间:2019-02-01 07:19:38

标签: angular

控制台中的响应:

    Subscriber {closed: false, _parent: null, _parents: null, _subscriptions: Array(1), syncErrorValue: null, …}
    closed: true
    destination: SafeSubscriber {closed: true, _parent: null, _parents: null, _subscriptions: null, syncErrorValue: null, …}
    isStopped: true
    syncErrorThrowable: true
    syncErrorThrown: false
    syncErrorValue: null
    _parent: null
    _parentSubscription: null
    _parents: null
    _subscriptions: null
    __proto__: Subscription

register.components.ts

    CandidateRegisterRequest() {
        let username = this.registermodel.username;
        let email = this.registermodel.email;
        let phone_number = this.registermodel.phone_number;
        let password = this.registermodel.password;

        // let id = this.commonuserservice.getSession('employerLoginId');
        let body = JSON.stringify({ username: username, email: email, phone_number: phone_number, password: password });
        let service_response =  this.service.HandlePost(body);
        console.log(service_response)

    }

auth.service.ts

    import { Injectable } from '@angular/core';
    import { HttpClient, HttpErrorResponse } from '@angular/common/http';
    import { catchError } from 'rxjs/operators';

    @Injectable({
    providedIn: 'root'
    })
    export class AuthService {
    private url = 'http://127.0.0.1:8000';
    private response_code = null;
    private response_msg = null;

    constructor(private http: HttpClient) { }

    HandlePost(post){
        return this.http.post(this.url, post)
        .subscribe(response => {
        this.response_code = response['code'];
        this.response_msg = response['msg'];
        return response;
        })
    }
    }

在这里,我尝试通过 POST 方法从服务文件(角度)发送数据 但我没有得到实际答复。

相反,我的反应超越了。

期望的响应: {“代码”:200,“ msg”:“成功”}

当我在组件中进行api调用而不是在服务文件上进行响应时,得到的响应是相同的。

这里我只是在服务中进行api调用,并向组件发送确切的响应。

但是得到了一些未知的响应。

请看看。

3 个答案:

答案 0 :(得分:1)

您需要subscribe到服务的Observable方法返回的HandlePost(post)

CandidateRegisterRequest() {
        /****
           other stuff
         ****/

        this.service.HandlePost(body).subscribe((response) => {

        let response_code = response['code'];
        let response_msg = response['msg'];
           /***
              do some other stuff with service response
           ***/
        });


}

在您的服务中,您需要返回http请求而不订阅它:

HandlePost(post){
        return this.http.post(this.url, post);
}

您可以查看官方HttpClient文档:

https://angular.io/guide/http

答案 1 :(得分:0)

您更新的 register.components.ts 代码如下。角发布方法返回一个Observable,即您需要订阅才能获取数据,错误...

CandidateRegisterRequest() {
        let username = this.registermodel.username;
        let email = this.registermodel.email;
        let phone_number = this.registermodel.phone_number;
        let password = this.registermodel.password;

        // let id = this.commonuserservice.getSession('employerLoginId');
        let body = JSON.stringify({ username: username, email: email, phone_number: phone_number, password: password });
        let service_response;
        this.service.HandlePost(body).subscribe((res)=>{
         service_response = res;
        });
        console.log(service_response)
    }

答案 2 :(得分:0)

您需要订阅您的HandlePost(post)方法返回的Observable  服务。

HandlePost(post): Observable<any>{
 return this.http.post(this.url, post);
}

然后订阅可观察的

this.service.HandlePost(body).subscribe((res)=>{
 service_response = res;
});

如果您想放置httpoptions,请同时添加它

HandlePost(post): Observable<any>{
 return this.http.post(this.url, post, httpOptions);
}