为什么我的 js 不将 PHP 输出读取为 JSON


why my js doesn't read php output as json

我正在使用AngularJs和php,我创建了一个返回Json格式的php文件,但是当涉及到从我的js文件中读取它时,输出是php代码!

服务器在 php 上运行良好,php 文件完美地被执行并返回 json 格式,但是当我在应用程序中调用它时,输出是我的 php 文本女巫意味着 php 文件被芦苇为文本。

我应该怎么做才能使其可读为 json?

这是我的 anjularJs 调用:

$http.get( './templates/connect.php').success(
function(data){
  $scope.cityes=data;  
  alert($scope.cityes); 
});

这是我的 php 文件:

$qry = mysql_query('SELECT capital from countries');
$emparray = array();
while ($rows = mysql_fetch_array($qry)) {
    $emparray[] = $rows;
}
header('Content-type:application/json;charset=utf-8');
echo json_encode($emparray);
确保您的 PHP

文件以<?php开头,并且您的服务器中启用了 PHP 处理器:

<?php
$qry = mysql_query('SELECT capital from countries');
$emparray = array();
while ($rows = mysql_fetch_array($qry)) {
    $emparray[] = $rows;
}
header('Content-type:application/json;charset=utf-8');
echo json_encode($emparray);

有关 PHP 语法及其开始/结束标记的更多信息。