Andurit
Andurit

Reputation: 5762

Angular do not parse API correctly ( PHP on server site )

I ask my API for data as it used to be, I think my API is not displaying data correctly but I can't find what I am doing wrong.

Here is my code

JS looks like:

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 looks like:

     $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);

and data which comes looks like:

{"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"}

However, I get this error:

Error: [orderBy:notarray] Expected array but received: {"data":"Array","status":200,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"http://example.com/api/v1/Soap.php?vin=VF38BRHZE80728805","headers":{"Accept":"application/json, text/plain, /"}},"statusText":"OK"}

Error expect problem on this line: <tr ng-repeat-start="soapCase in soapCases | orderBy:sortField:reverse">

It says it expects an array but didn't get it. I really don't think it should expect an array instead of JSON.

Any advice as to what I'm doing wrong?

EDIT: I have one similar function getdata() function that looks like:

function getData(){
   var myPromise = $http({
        method: 'GET',
        url: 'http://www.audahistory.cz/api/v1/History.php?vin=' + $scope.vin
    });
    return myPromise;
};

with result: http://pastebin.com/s31jhnip but this works correctly

Upvotes: 0

Views: 206

Answers (1)

scareddragon
scareddragon

Reputation: 445

In your PHP code you're building an array, but indexed with strings, so it gets converted to a JSON object. ng-repeat expects array, so someting like:

[{"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"}]

So in PHP you should insert this associative array into normal array:

$result = array();
$result[] = $data;

And then try to convert it into JSON.

echo $this->convertToJson($result);

If there's no data returned you have two options:

  • Return an empty array converted to JSON
  • Return HTTP 404 error response and handle that in Angular.

Upvotes: 2

Related Questions