Create a post method inside a class with empty constructor

this is my class
.
export class publicip {
publicip: string;
constructor(private http:Http) {
//this.navCtrl.push(MainPage);
}

public getip(){
let headers = new Headers({‘Content-Type’:‘application/x-www-form-urlencoded’,‘Accept’:‘application/json’});
let options = new RequestOptions({headers:headers});
let credentials = “username=”+’’+"&password="+’’;
// index.php/rest_api_mobile/index_post
this.http.post(‘http://localhost/mechmarcl_website/index.php/Admin/getpublicip’, credentials, options).
map(res => res.json()).subscribe(
data => {

          if(data['status']==true{                     
            console.log(this.publicip);              
          }else{                              
         }
        },
        err => {
          console.log("ERROR!: ", err);
        }
  );

}
}

what should i pass as the parameter when i call to this getip method from outside.

this.public = new publicip(’-----).getip();

Did you write this code? If so, when? If the answer is “less than 3 years ago”, then I would suggest reading this and starting completely over. If you didn’t write it, I would suggest going and asking whoever did write it if there is a newer version. If there isn’t, I would suggest choosing a different person to rewrite the app.

Thanks for your reply. This code was written in 2018 and i refer the link that you have mentioned up there.

i change my method to this.

 public getip(){
    
        return this.http.get('http://localhost/mechmarcl_website/Test2/assets/Output.txt');
 }

and this is my constructor

 constructor(private http?: HttpClient){}

now im getting this error " Cannot read property ‘get’ of undefined "

Awesome! That looks radically better. I would get rid of the ? after the http parameter in the constructor. See if that helps.

1 Like