0

I have a server side REST api, which can return a jsonyfied data with arbitrary structure. On a client side I have an Angular application.

Resource is defined:

module.factory('SearchQueries', function ($resource){          
   return $resource('/instances/searches/:_id', {_id: '@_id'}); 
})                                                             

After I try to get data from my api:

$scope.search_queries = SearchQueries.query(null, function(args){  
  console.log('success: ', args);                                  
  for (i in args) {                                                
  }                                                                
}); 

The client side receives a data with kinda structure:

{"2": "serfgserg", "3": "sdfgdfg", "4": "sdgdfhdfghfgh", "5": "sdgdfhdfghfgh"}

But! In the success function of the Angular resource every symbol of the data is the value of one element object.

[{"0":"s","1":"e","2":"r","3":"f","4":"g", e.t.c

Why is it done? How to prevent or use it rightly?

edit:

If I return an array from the server:

res = json.dumps(["sdf", "asdf", "asdf"])                             
return Response(response=res, mimetype='application/json',
        status=status)

Never mind I have the same result:

[{"0":"s"},{"0":"d"},{"0":"f"},{"0":"a"},{"0":"s"},{"0":"d"},{"0":"f"} // e.t.c     
2
  • Does your server return about valid json? Does your server return back "application/json"? Commented Apr 18, 2013 at 21:36
  • Yes it is. Answer has edited. Commented Apr 19, 2013 at 7:30

1 Answer 1

1

This happends beacause query expects an array of objects, not an object - see the ngResource documentation:

'query': {method:'GET', isArray:true}

expects something like:

[{"name":"sdf"}, {"name":"asdf"}, {"name":"asd"}]
Sign up to request clarification or add additional context in comments.

3 Comments

I've clarified my answer, you should return an array of objects
Thanks! Why is it modified instead of just get data or except it if data format is not valid? This is strange for me and I really want to know what for is it done?
A resource doesn't really make sense unless it's an object. Every object in your array will inherit the SearchQueries prototype object (with methods as $save and $delete). You should also check out the $http service.

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.