2017-06-23 89 views
0

嗨,大家好,我使用Angular 4作为我的前端,从后端返回json,但不是纯json数据,它返回给我更多,我如何才能返回唯一的json?后端是问题还是我应该在前端做点什么?从PHP后端只返回JSON

我在后端使用笨这里面的方法:

$this->FAM->list_app(); 

从模型中,这已经实现json_encode()

,并从模型的后端:

$arrayindex=array(); 

foreach($query->result_array() as $r){ 
    $arrayindex[] = $r; 
} 

echo json_encode($arrayindex); 

enter image description here

+0

显示你的PHP代码 – shubham715

+0

' .MAP(RES => res.json())' – developer033

+0

9倍10,你的php代码将回显一个php数组,如'json_encode($ phpArr);' –

回答

1

你可以做t他对前端服务使用res.json(),然后访问它的body

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); 
    } 
+0

谢谢这位老兄,但是它回复空 – Arcubalino

+0

因为你的回复没有正文,只是做数据 – Sajeetharan

+0

哈哈,@S ajeetharan –