0

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

8
  • $scope.soapCases = JSON.parse(soapData); Commented Dec 14, 2015 at 13:05
  • hey @Steve thanks for answer, I just try it with same result :( basicly I have same function for getData() just another endpoint and works correctly, result from that endpoint can be found here: pastebin.com/s31jhnip Commented Dec 14, 2015 at 13:06
  • How about $scope.soapCases.push(soapData); - its not exacly clear what you are trying to do, replace or add to $scope.soapCases Also the names are weird, this clearly isnt soap, its standard json Commented Dec 14, 2015 at 13:10
  • Are, just saw your edit, i know what the problem is - the above code sends a single json object, not an array. Either use the push code in my above comment, or send a single element array in the php: $data = array (array( "caseNumber" =>$claim['@attributes']['id'], ... )); Commented Dec 14, 2015 at 13:12
  • @Steve name is Soap because data come from SOAP but it's parsed by PHP before. Commented Dec 14, 2015 at 13:12

1 Answer 1

2

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.
Sign up to request clarification or add additional context in comments.

7 Comments

Hi @scareddragon I really appriciate your help however this return same error as before :(
Can you paste JSON response from the API after the change?
sure, json can be found here: pastebin.com/DYEkrQpp Error is displayed even if server return blanked result ( no data for specific VIN )
This looks ok. How does this output look when there's no data?
blanked response no data nothing at all :) Isn't it double array problem on that pastebin?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.