Hi guys i'm using Angular 4 for my front end to return json from backend but instead of pure json data it return me more, how can i return only json? Is the problem in the backend or should i do something in the front end?
I'm using codeigniter in the backend this inside the method:
$this->FAM->list_app();
from the model this already implementing json_encode()
and from the model backend:
$arrayindex=array();
foreach($query->result_array() as $r){
$arrayindex[] = $r;
}
echo json_encode($arrayindex);
You can do this on the frontEnd service by using res.json()
and then access the body
of it,
yourService(): Observable<any> {
return this._http.get('url')
.map(this.success)
.catch(this.fail);
}
private success(res: Response) {
if (!res || res.status !== 200) {
return [];
}
const data = res.json();
const results = data ? (data.message ? data.message : []) : [];
//return results;
return data;
}
private fail(error: Response | any) {
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
}