2017-10-26 4 views
1

Ich habe ein Problem über Service in Angular2/4. Ich erhalteService von Angular 4 und CodeIgniter 3

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

Das ist mein Back-End mit CI. Dies ist mein Controller 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)); 
} 

Das Modell in CI:

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

Und das ist mein Dienst in Angular 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()); 
    } 


} 

Das ist mein json von CI ist:

'{"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"}]}' 

Ich nehme an, dass das Anführungszeichen (') in meinem JSON, dass meine Apps Fehler machen. Wie Sie diese Zitate entfernen?

Antwort

0

Richtiger Weg json reponse von CI Controller ist zu senden:

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

wow hilfreiche dank btw .. – Arza

+0

hey kann ich dir etwas fragen, jetzt mein Frontend (Winkel-) diesem Fehler geben „ERROR Fehler: Kann kein unterstützendes Objekt '[Objekt Objekt]' vom Typ 'Objekt' finden. NgFor unterstützt nur die Bindung an Iterables wie Arrays. " du weißt, warum ? – Arza

+0

https://stackoverflow.com/questions/46951513/angular-4-cannot-find-a-differ-supporting-object-object-object-of-type-ob das ist der Link sir – Arza