Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient Angular 5 does not send request

I have a problem with HttpClient in Angular 5. HttpClient does not send any request (I don't see any xhr log in console) on two specified components. On the others components everything is fine.

Calling ApiService POST method (custom service which works like a wrapper for HttpClient) from Component A, but when I call this method from Component B HttpClient seems to be frozen.

There are many components in my app that use ApiService. Everything is injected fine. I have no idea what is wrong.

--- respond

ApiService.ts

@Injectable()
export class ApiService
{
    private errorListeners : Map<string, Array<(details ?: any) => any>> =
        new Map<string, Array<(details ?: any) => any>>();

    public constructor(private http: HttpClient)
    {

    }

    public post<T>(path : string, data : any, urlParams : any = null) : Observable<any>
    {
        return this.http.post<T>(`${environment.api.path}${path}`, data, {
            params: urlParams
        }).catch(this.catchErrors()).map(response => {
            if (response['Error']){
                throw response['Error'];
            }

            return response;
        });
    }

}

-- Component

   @Component({
    selector: 'login-register-component',
    templateUrl: './register.component.html',
    styleUrls: [
        './../../assets/main/css/pages/login.css'
    ]
})
export class RegisterComponent implements OnInit, OnDestroy
{

public constructor(private route: ActivatedRoute,
                       private router: Router,
                       private userService : UserService,
                       private apiService: ApiService
    )
    {
        this.apiService.post('/some-endpoint', null, {}).subscribe(res => {
console.log(res);

});

}

HttpClient does not work even if i directly inject HttpClient into Component

-- Other component in the same module example call: (it works)

public loginTraditionalMethod(emailAddress : string, plainPassword : string)
    {

        this.apiService.post('/auth/email', {
            email: emailAddress,
            password: plainPassword
        }, {}).subscribe(res => {
           console.log(res);
        })

    }
like image 225
Reverze Avatar asked Jan 28 '23 17:01

Reverze


1 Answers

I was having the same problem, no xhr request after subscribing to a http.get(). This was a request for a forgotten password function, I was therefore not connected to the app.

The request was being intercepted by an http token interceptor that was returning an empty Observable if no session was detected.

Never know, this might help someone...

like image 66
Laurie Clark Avatar answered Jan 31 '23 07:01

Laurie Clark