Radeontrax office
Radeontrax office

Reputation: 33

How can a variable be passed in http URL request in ionic 4 with angular?

How to pass variable as parametre in a http request in ionic 4 with angular?


 response: any;

 constructor(private http: HttpClient) { }

async logIn(Companyid: string, Userid: string, Password: string, lat: any, lot: any, deviceid: string) {


 console.log(Companyid, Userid, Password);
 this.http.get('https://www.service.com/App_1.3.5/login.php',
)
  .subscribe(data => {
   this.response = data;
   });

   // console.log(this.response);

   // return this.response
 }
}

I tried as below but its passing as string

export class UrlService {

  response: any;

  constructor(private http: HttpClient) { }

 async logIn(Companyid: string, Userid: string, Password: string,
 lat: any, lot: any, deviceid: string) {

  // tslint:disable-next-line:whitespace
  // const httOptions = ;
  console.log(Companyid, Userid, Password);
  // tslint:disable-next-line:max-line-length
  this.http.get('https://www.service.com/App_1.3.5/login.php?compid=Companyid&username=Userid&password=Password&deviceID=deviceid&latitude=lat&longitude=lot')
   .subscribe(data => {
    this.response = data;
    });

    // console.log(this.response);

    // return this.response
  }
}

So plz help me with how to pass variables as parameters in a http URL request

Upvotes: 2

Views: 833

Answers (3)

Vishwa Deepak Singh
Vishwa Deepak Singh

Reputation: 453

Eg you have LoginPage now go to login.page.ts file and write below code:

 class LoginPage {
    constructor(private loginService: LoginService)
      async login(){
         this.loginService.userLogin(companyid,userId,password,latitude,longitude,deviceId)
.then((data)=>{
                    console.log(data)
                },(err)=>{
                    console.log('error',err);
            })
       }
  }

Now go to your service and write this below code

 async userLogin(companyId,userId,password,lat,log,deviceId){
 const url = 'https://www.service.com/App_1.3.5/login.php';
 let params: any = {
      companyid: companyId,
      userid: userId,
      password: password,
      latitude:lat,
      longitude:log,
      deviceId: deviceId
    };
     return this.httpClient
      .get(url, params)
      .then(data => data);
 }

I think this will solve your problem.

Upvotes: 0

Jejouz
Jejouz

Reputation: 179

You can construct the url with the parameters before passing it to your request:

const baseURL = 'https://www.service.com/App_1.3.5/login.php?';
const queryOptions = 'compid=' + Companyid + '&username=' + Userid; //Keep going
let requestUrl = `${baseURL}${queryOptions}`;

 this.http.get(requestUrl)
  .subscribe(data => {
   this.response = data;
 });
 }

Upvotes: 1

Gvs Akhil
Gvs Akhil

Reputation: 2634

You are passing string directly instead of referring to variables, try this:

this.http.get(`https://www.service.com/App_1.3.5/login.php?compid=${Companyid}&username=${Userid}&password=${Password}&deviceID=${deviceid}&latitude=${lat}&longitude=${lot}`)
       .subscribe(data => {
        this.response = data;
        });

        // console.log(this.response);

        // return this.response
      }

Note: Notice that ` is not a single quotation and its tilde in keyboard

Upvotes: 1

Related Questions