0

Let's say a Service like this:

.factory('GetPaths', function($resource) {
  return $resource('/paths/coord/:lat/:long', 
       {lat: "@lat" }, {long: "@long" } , 
       {
         get:    {method: 'GET' , isArray: true },  
      }

   );
})

it gets called like this:

scope.paths = GetPaths.get({'lat':1},{'long':1}); 

I get the following response:

{"long":1,"$promise":{},"$resolved":false}

When I use cURL on my backend, I get this JSON Array:

[
  {
    "pathid":"1",
    "title":"Pathest",
    "eta":"3:00",
    "TYPE":"Hike",
    "difficulty":"Hard",
    "distance_in_km":"0"
 }
]

My Question is: How to retrieve the same JSON Array as data to populate my list?

Wheatley
  • 153
  • 1
  • 12

2 Answers2

0

There is an issue with $resource configuration. Should be:

return $resource('/paths/coord/:lat/:long', 
   {lat: '@lat', long: '@long' } , 
   { get: { method: 'GET', isArray: true } }
);

And in your case you can just use query instead of get and leave out third argument at all. Check out $resource docs.

Dmitry Evseev
  • 11,533
  • 3
  • 34
  • 48
  • is it normal that my log now returns an empty response? – Wheatley Oct 20 '14 at 21:50
  • Just to be sure, after applying the fix to config you're doing the call to api like `GetPaths.query({ lat: 1, long: 1 })`? – Dmitry Evseev Oct 20 '14 at 22:01
  • Yes. my new code is available here [JS Pastebin](http://pastebin.com/h3yV0xQ2) you can call the API on [API Sample Call](http://p-express.ddns.net/ike/v1/paths/coord/1/1) – Wheatley Oct 20 '14 at 22:14
  • Yes, it's probably fine because `$resource` is asynchronous. You can check if data becomes available to the controller, e.g. by outputting `
    {{ paths | json }}
    ` in the template.
    – Dmitry Evseev Oct 20 '14 at 22:18
  • Or use alternative callback syntax for $resource: `GetPaths.query({'lat':1,'long':1}, function (data) { $scope.paths = data; console.log('pats', data); });` – Dmitry Evseev Oct 20 '14 at 22:20
  • I tried the Alternative Callback Syntax, the log is empty (i.e. data is never available for the controller?) – Wheatley Oct 20 '14 at 22:27
  • my JavaScript Console gives this `XMLHttpRequest cannot load http://p-express.ddns.net/ike/v1/paths/coord/1/1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.` – Wheatley Oct 20 '14 at 23:40
  • You need CORS to be configured on the API https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS – Dmitry Evseev Oct 21 '14 at 07:22
0

The problem comes Actually from My Browser's CORS Security. it could be disabled through opening chrome with:

--args --disable-web-security
Wheatley
  • 153
  • 1
  • 12