角2访问PHP API,而不是返回JSON,但返回文件

问题描述:

角2访问与此代码我的PHP文件:角2访问PHP API,而不是返回JSON,但返回文件

this._http.get('../api/src/1.0/get/images.php').subscribe(res => { 
    alert(JSON.stringify(res)); 
}); 

哪个给出了这样的警告本地主机:3000说

{ 
    "_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文件

<?php 

require_once '../../orm/connection.php'; 

$images = R::getAll('SELECT * FROM image'); 
header('Content-Type: application/json'); 
echo json_encode($images); 

?> 

我期待警报只包含JSON数据。 如果我通过它的网址去直接将文件只给我的JSON数据。 我将如何实现这一目标?

要调用的位置../api/src/1.0/get/images.php静态文件,你会得到它的内容。

您必须运行与服务器的PHP文件,比http.get(...)调用它可以用PHP本地主机服务器php -S localhost:3333,你需要在你的项目的根目录运行此命令(PHP项目)

+0

所以你要说的是PHP运行本地主机的默认端口和2角运行本地主机端口3000,我需要在端口3000上运行PHP?我在哪里可以使用php -S localhost:3333以及为什么使用3333? – ClickThisNick

+0

不好意思啊,那是只例如:3333,你可以使用任何...最好是同时运行客户端应用程序(角2)和API(PHP)与同一个服务器,只是在不同的文件夹... –

+0

有道理。一旦我去我的默认端口上的网站工作正常! – ClickThisNick

从你的反应得到,它看起来像您请求images.php"Content-Type":["application/octet-stream"]。尝试配置http.get()索要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)); 
}); 
+0

嗯仍与此头相同 – ClickThisNick