douhui1333
2017-06-23 04:32
浏览 35
已采纳

从PHP后端仅返回JSON

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);

enter image description here

图片转代码服务由CSDN问答提供 功能建议

大家好我正在使用Angular 4为我的前端从后端返回json而不是纯json数据 还给我更多,我怎么能只返回json? 是后端的问题还是我应该在前端做些什么?

我在方法的后端使用codeigniter:

  $ this-> FAM-> list_app()  ; 
   
 
 

来自已经实现 json_encode()

的模型以及模型后端 :

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhou5344 2017-06-23 04:35
    已采纳

    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);
        }
    
    点赞 打赏 评论

相关推荐 更多相似问题