HttpClient not working


#1

Hi,

I have a problem, I use httpclient in my project, but this function not conect with my BE.

And not show ingerror in the terminal. I use angular 5.0.1

My code:

service.ts

import { Injectable} from "@angular/core";
import { Observable as RxObservable } from "rxjs/Observable";
import "rxjs/add/operator/map";
import { HttpClient, HttpHeaders, HttpResponse } from "@angular/common/http";
import * as ApplicationSettings from "application-settings";
import { GlobalService } from "./global.service";




@Injectable()
export class SecurityService {

    private url: string;

    constructor(private http: HttpClient, private globals: GlobalService) {
        this.url = `${this.globals.getUrl()}`;
    }

    private createRequestHeader() {
        let headers = new HttpHeaders();
        let token = this.globals.getToken();
        if (token != "") {
            headers.set("X-AUTH-TOKEN", token);
        }
        headers.set("Accept", "application/json");
        //headers.set("AuthToken", "my-token");
        headers.set("Content-Type", "application/json");
        return headers;
    }


    userlogin(data: any) {
        let url = `${this.url}security/userlogin`;
        console.log(url);
        let options = this.createRequestHeader();
        return this.http.post(url, { data }, { headers: options })
            .map(res => res);
    }

}


component.ts

    public login() {
        if (this.input.user != "" && this.input.password != "") {
            let data = { "username": this.input.user, "password": this.input.password, "channelId": "101" };
            this._securityService.userlogin(data).subscribe(res => {
                this.result = (<any>res).json.data;
                console.log(this.result);
            });
        } else {
            (new SnackBar()).simple("All Fields Required!");
        }
    }

Please help me


#2

Have you imported NativeScriptHttpClientModule in your app module.

import { NativeScriptHttpClientModule } from "nativescript-angular/http-client";
...
imports:[
...
NativeScriptHttpClientModule
]
...

#3

Thanks!!! You resolve my problem!