我向我的API询问过去的数据,我认为我的API没有正确显示数据,但我找不到我做错了什么。
这是我的代码
JS看起来像:
function getSoapData(){
var myPromise = $http({
method: 'GET',
url: 'http://example.com/api/v1/Soap.php?vin=' + $scope.vin
});
return myPromise;
};
$scope.doWE = function(){
getData().success(function(data){
console.log(data);
$scope.cases = data;
getSoapData().then(function(soapData){
$scope.soapCases = soapData;
console.log(soapData);
});
});
};
PHP看起来像:
$data = array (
"caseNumber" =>$claim['@attributes']['id'],
"date_created" =>$claim['country'],
"country" =>$claim['creation'],
"currency" =>$claim ['specific']['currency'],
"insurer_memberid" =>$claim['insurance']['id'],
"laborcosts" =>$claim ['specific']['partsCost'],
"model" =>$claim ['specific']['model_name'],
"orgName" =>$claim['insurance']['name'],
"paintLabor" =>$claim['specific']['paintmaterial'],
"totalcosts" =>$claim ['assessment']['damage-value']
);
echo $this->convertToJson($data);
数据看起来像:
{"caseNumber":"2003-09-30.BEL.BE01001129143","date_created":"BEL","country":"2003-09-30","currency":null,"insurer_memberid":"1671","laborcosts":null,"model":null,"orgName":"ZELIA INSURANCE","paintLabor":null,"totalcosts":"11157.02"}
然而,我得到了这个错误:
错误:[orderBy:notarray]应为数组,但收到的却是:{"data":"Array","status":200,"config":{"method":"GET","transformRequest":[null],"transformaResponse":[noll],"url":http://example.com/api/v1/Soap.php?vin=VF38BRHZE80728805","headers":{"Accept":"application/json,text/plain,/"}},"statusText":"OK"}
这一行出现错误预期问题:<tr ng-repeat-start="soapCase in soapCases | orderBy:sortField:reverse">
它说它需要一个数组,但没有得到。我真的不认为它应该期望一个数组而不是JSON。
关于我做错了什么,有什么建议吗?
编辑:我有一个类似的函数getdata()
函数,看起来像:
function getData(){
var myPromise = $http({
method: 'GET',
url: 'http://www.audahistory.cz/api/v1/History.php?vin=' + $scope.vin
});
return myPromise;
};
结果:http://pastebin.com/s31jhnip但这是正确的
在PHP代码中,您正在构建一个数组,但使用字符串进行索引,因此它被转换为JSON对象。ng repeat需要数组,所以有点像:
[{"caseNumber":"2003-09-30.BEL.BE01001129143","date_created":"BEL","country":"2003-09-30","currency":null,"insurer_memberid":"1671","laborcosts":null,"model":null,"orgName":"ZELIA INSURANCE","paintLabor":null,"totalcosts":"11157.02"}]
因此,在PHP中,您应该将这个关联数组插入到普通数组中:
$result = array();
$result[] = $data;
然后尝试将其转换为JSON。
echo $this->convertToJson($result);
如果没有返回数据,您有两个选项:
- 返回转换为JSON的空数组
- 返回HTTP 404错误响应,并在Angular中进行处理