Home » Php » service from Angular 4 and CodeIgniter 3

service from Angular 4 and CodeIgniter 3

Posted by: admin October 26, 2017 Leave a comment

Questions:

I have a problem about service in angular2/4.
I keep getting”ERROR SyntaxError: Unexpected token ‘ in JSON at position 0″

this is my backend using CI
this is my 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));
}

the model in CI

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

and this is my service 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());
    } 


}

thats is my json from CI

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

i assume that quotation mark (‘) in my json that make my apps error.
how to remove that quotations thanks

Answers:

try this code to add the json response header and display json data:

//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";
    }

    header('Content-Type: application/json');
    echo json_encode($output); die;
}

Questions:
Answers:

Proper way to send json reponse from CI Controller is :

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
hvaen’t defined the header type.

Questions:
Answers:

Problem is as you said in the apostrophes surrounding your JSON. You should return valid JSON from your backend, so proper solution is to solve it there.

Not sure about code igniter, but it should be enough to just echo the JSON.

//send response
echo json_encode($output);

Or you could trim it on your client with text.substr(1, text.length - 2), but that is not a good solution, as you will not be able to leverage automatic JSON conversion in HttpClient (replacement for Http, that gets deprecated).