So I'm trying to get JSON data from php file but the console shows me this error:
EXCEPTION: Unexpected token < in JSON
I just sent a simple json array via php like this:
<?php
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: X-Requested-With');
header('Content-Type: application/json');
$res = [
array(
'title' => 'First task',
'description' => 'skdfjsdfsdf',
'done' => false,
),
array(
'title' => 'Second task',
'description' => 'skdfjsdfsdf',
'done' => false,
),
array(
'title' => 'Third task',
'description' => 'skdfjsdfsdf',
'done' => false,
)
];
echo json_encode(array('tasks' => $res));
This is the location of my php file:
And finaly this is my service class:
import { Injectable } from '@angular/core';
import {Http, Headers} from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class TasksDataService {
constructor(private http: Http) {}
getTasks(){
return this.http.get('http://localhost:4200/src/database.php')
.map(res => {
console.log(res.json());//--I get the error in this line
var result = res.json().tasks;
console.log(result);
return result;
});
}
}
I really googled a lot for this problem and tried a lot of solutions but still getting the same error !