Reputation: 5250
Angular 2 is accessing my php file with this code:
this._http.get('../api/src/1.0/get/images.php').subscribe(res => {
alert(JSON.stringify(res));
});
Which gives this alert LocalHost:3000 Says
{
"_body": "<?php\n\nrequire_once '../../orm/connection.php';\n\n$images = R::getAll( 'SELECT * FROM image');\nheader('Content-Type: application/json');\necho json_encode($images);\n\n?>\n",
"status": 200,
"statusText": "Ok",
"headers": {
"Date": ["Sat",
" 13 Feb 2016 21:11:26 GMT"],
"Cache-Control": ["public",
" max-age=0"],
"Last-Modified": ["Sat",
" 13 Feb 2016 20:39:49 GMT"],
"Accept-Ranges": ["bytes"],
"ETag": ["W/\"a7-152dc5c6608\""],
"Content-Length": ["167"],
"Content-Type": ["application/octet-stream"]
},
"type": 2,
"url": "http://localhost:3000/api/src/1.0/get/images.php"
}
PHP File
<?php
require_once '../../orm/connection.php';
$images = R::getAll( 'SELECT * FROM image');
header('Content-Type: application/json');
echo json_encode($images);
?>
I was expecting the alert to only contain the JSON data. If I go to the file directly via its url it only gives me the JSON data. How would I achieve this?
Upvotes: 1
Views: 994
Reputation: 41294
From the response you are getting, it looks like you are requesting images.php
as "Content-Type":["application/octet-stream"]
. Try configuring http.get()
to ask for json:
const HEADER = { headers: new Headers({ 'Content-Type': 'application/json' }) };
this._http.get('../api/src/1.0/get/images.php', HEADER).subscribe(res => {
alert(JSON.stringify(res));
});
Upvotes: 1
Reputation: 6424
You are calling static file at location ../api/src/1.0/get/images.php
and you get it's content.
You must run your .php file with server, than call it with http.get(...)
You can use php localhost server with php -S localhost:3333
you need to run this command in your project root ( php project )
Upvotes: 1