2017-10-26 9 views
1

Angular2/4でサービスに関する問題があります。 私は、これはCIを使用して、私のバックエンドですAngular 4とCodeIgniter 3のサービス

ERROR SyntaxError: Unexpected token ' in JSON at position 0.

を得続けます。 これは私のコントローラbook.phpです:

//get book from database 
function list_book_get() 
{ 
    //call method get_list_book() in model m_book 
    $dataBook = $this->m_book->get_list_Book(); 

    //if dataBook exist 
    if($dataBook != null) { 
     $output['status'] = true; 
     $output['data'] = $dataBook; 
    } else { // if dataBook not exist 
     $output['status'] = false; 
     $output['message'] = "empty"; 
    } 

    //send response 
    $this->response(json_encode($output)); 
} 

CIでのモデル:

function get_list_book() { 
     return $this->db->get('book')->result(); 
    } 

そして、これは角4で私のサービスです。

import { Injectable } from '@angular/core'; 
import { Http } from '@angular/http'; 
import 'rxjs/add/operator/map' ; 

@Injectable() 
export class BookService { 

    constructor(private http: Http) { 

    } 

    books=[]; 
    getBooks(){ 
     return this.http.get("http://localhost/restserver/book/list_book.php") 
     .map(res => res.json()); 
    } 


} 

CIからの私のJSONです:

'{"status":true,"data": 
[{"id":"1","title":"SAINS","author":"ERLANGGA","isbn":"089928778"}, 
{"id":"2","title":"Geography","author":"ERLANGGA","isbn":"089182372"}, 
{"id":"3","title":"Math","author":"Srilangka","isbn":"091283181"}, 
{"id":"4","title":"test","author":"test","isbn":"1283798127"}, 
{"id":"5","title":"AAAA","author":"BBB","isbn":"91092301290"}, 
{"id":"6","title":"BBB","author":"CCC","isbn":"01920192"}]}' 

私は自分のアプリケーションにエラーが発生する私のjsonの引用符( ')と仮定します。 引用を削除するには? CI ControllerからのJSON REPONSEを送信するために

答えて

0

適切な方法は次のとおりです。

function list_book_get() 
{ 
    ... 
    return $this->output 
       ->set_content_type('application/json') 
       ->set_output(json_encode($output)); 
} 

You are getting json in single quotes bcoz (means as string) , you haven't defined the content type.

+0

すごい、本当に参考になっおかげで、ところで... – Arza

+0

ちょっと私はあなたに何か、今、私のフロントエンド(角度)は、このエラーを与える「ERRORを求めることができますエラー:タイプ 'object'の異なるサポートオブジェクト '[object object]'が見つかりません。NgForは配列などのIterablesへのバインドのみをサポートしています。あなたが理由を知っている ? – Arza

+0

https://stackoverflow.com/questions/46951513/angular-4-cannot-find-a-differ-supporting-object-object-object-of-type-ob thatsリンク先 – Arza