2

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

2
  • what final url you want to make with values of variables? Commented Jan 30, 2020 at 8:00
  • Tq I solved the issue : solution is as follow: this.http.get('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; });**using + symbol I concortinated the variables with the request URL** Commented Jan 30, 2020 at 9:05

3 Answers 3

1

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;
 });
 }
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.