AJAX Call ANGULAR 5

Hello i am trying to make a background “Silent” call to webservice in order to get a token
i am struggling with ajax.
I studied an example that i found in Angular 1 which uses Ajax for some reason
and because i am newbie i decided to immitate the same code in angular 5 but i getting issues.

here is my code @ serviceprovider.ts

import { HttpClient } from ‘@angular/common/http’;
import { Injectable } from ‘@angular/core’;
import ‘rxjs/add/operator/map’;
import {Observable} from “rxjs/Observable”;
import ajax // ERROR here
var token ="";

@Injectable()
export class LoginserviceProvider {

url=‘http://192.168.0.6:81/EBSWebApi’;
token="";

constructor(public http: HttpClient) {
console.log(‘Hello LoginserviceProvider Provider’);
this.url=‘http://192.168.0.6:81/EBSWebApi’;
}

ready(){
ajax({
url:this.url+ “api/login”,
type:“POST”,
data:JSON.stringify({
SubscriptionPassword: “passx”,
model: {
UserID: “khcadmin”,
UserPassword: “00100100957”,
BranchID: “01”,
AllowHttp: true
}
}),
contentType:“application/json; charset=utf-8”,
success:function (result) {
token = result.Model.WebApiToken;
document.getElementById(“token”).innerText=token;

  }
})

}

Hi @nbenakis

Angular5 is entirely different from Angular1 . You don’t have to make ajax. Use Angulars HttpClient.

You can do this

data = yours json data;
this.http.post('url', data)
    .map(res => res.json())
    .subscribe(res => {
         console.log(res);
    }, err => {});

–> folder /provider/loginservice.ts

import { HttpClient } from ‘@angular/common/http’;
import { Injectable } from ‘@angular/core’;
import ‘rxjs/add/operator/map’;
import {Observable} from “rxjs/Observable”;

let token ="";

@Injectable()
export class LoginserviceProvider {

url=‘http://192.168.0.6:81/EBSWebApi’;

params={ SubscriptionPassword: “passx”,
UserID: “khcadmin”,
UserPassword: “00100100957”,
BranchID: “01”,
AllowHttp: true
};

constructor(public http: HttpClient) {
console.log(‘Hello LoginserviceProvider Provider’);
this.url=‘http://192.168.0.6:81/EBSWebApi/api/login’;
}

getResponse():Observable{
this.params
return this.http.post(“http://192.168.0.6:81/EBSWebApi”,this.params)
}
}

–> app/pages/home/home.ts

import { Component } from ‘@angular/core’;
import { NavController } from ‘ionic-angular’;
import {LoginserviceProvider} from “…/…/providers/loginservice/loginservice”;

@Component({
selector: ‘page-home’,
templateUrl: ‘home.html’
})
export class HomePage {
params;

constructor(public navCtrl: NavController,
private loginserviceprovider: LoginserviceProvider) {
}

getFeedback(){
this.loginserviceprovider.getResponse()

  .subscribe(data=>{
    console.log(data);
  });

}

i am still not getting any token as response

any error response from server?.

.subscribe(data=>{
  console.log(data);
}, err => {
   console.log(err);
});