Hi, I am new with ionic 3 and I am using Ionic View on device and i have my application showing but when i try to get data using http from a REST Api nothing happen.
Can any body help?
Thanks
Hi, @rothdevcon
Try Like below code :
import { Injectable } from '@angular/core';
// import { Http } from '@angular/http';
import { Http , Headers, RequestOptions} from '@angular/http';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
export class RequestProvider {
hostname:string = "http://localhost:8080";
constructor(public http: Http) {
console.log('Hello RequestProvider Provider');
}
get(endPoint){
let url = this.hostname + endPoint;
var promise = new Promise((resolve,reject)=>{
this.http.get(url).map((res)=> res.json())
.subscribe(data => {
resolve(data);
})
})
return promise
}
post(endPoint,data){
let url = this.hostname + endPoint;
let headers = new Headers();
headers.append('Access-Control-Allow-Origin' , '*');
headers.append('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
headers.append('Accept','application/json');
headers.append('content-type','application/json');
let options = new RequestOptions({ headers:headers});
return new Promise((resolve,reject)=>{
this.http.post(url,JSON.stringify(data), options).subscribe(res => {
resolve(res.json());
}, (err) => {
reject(err);
});
})
}
}
Thanks
1 Like