Php 角度返回数据
我正在尝试创建一个服务,该服务将从PHPAPI返回json数据,但angular似乎不是返回纯json数据,而是返回json及其配置 services.jsPhp 角度返回数据,php,angularjs,Php,Angularjs,我正在尝试创建一个服务,该服务将从PHPAPI返回json数据,但angular似乎不是返回纯json数据,而是返回json及其配置 services.js .service('DistinctAPIService', function($http){ var base = 'http://localhost/onseral/api/'; this.listDistinct = function(table, field){ return $http({ method
.service('DistinctAPIService', function($http){
var base = 'http://localhost/onseral/api/';
this.listDistinct = function(table, field){
return $http({
method: 'POST',
url: base + '/listDistinct.php',
params: {
table: table,
field: field
}
});
}
})
.controller('DistinctMatcode', function($scope, DistinctAPIService){
DistinctAPIService.listDistinct('material', 'matcode').then(function(data){
$scope.data = data;
console.log(JSON.stringify(data));
})
})
listdistinct.php
<?php
require_once '/config/dbconfig.php';
$table = $_GET['table'];
$field = $_GET['field'];
GetData($table,$field);
function GetData($tablename,$fieldname) {
$sql = "SELECT DISTINCT $fieldname as expr1 FROM $tablename order by expr1 asc";
try {
$db = getdb();
$stmt = $db->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode(array('data' => $data));
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
?>
有什么想法吗?试试这个
controller('DistinctMatcode', function($scope, DistinctAPIService){
DistinctAPIService.listDistinct('material', 'matcode').then(function(response){
$scope.data = response.data.data;
console.log(JSON.stringify(data));
})
在将数据发送到客户端之前,请使用header功能。 我修改了您的listdistinct.php文件。试试看,如果问题仍然存在,请告诉我
<?php $sql = "SELECT DISTINCT $fieldname as expr1 FROM $tablename order by expr1 asc";
try {
$db = getdb();
$stmt = $db->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
header('Content-Type: application/json');
echo json_encode(array('data' => $data));
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
它确实删除了{“data”:
但仍然得到了另一个{“data”:
,我只想得到结果为“[{”expr1:“value”}]试试这个$scope.data=data.data.data。我很高兴帮助你。更好地使用这个格式。我编辑它。仍然一样。实际上hadiJZ已经给出了答案,谢谢
<?php $sql = "SELECT DISTINCT $fieldname as expr1 FROM $tablename order by expr1 asc";
try {
$db = getdb();
$stmt = $db->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
header('Content-Type: application/json');
echo json_encode(array('data' => $data));
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}