0

I have created two resources

angular.module('myApp.services', ['ngResource'])

.factory('Banana', function ($resource) {
    return $resource('http://localhost:8080/banana/:bananaId');
})

.factory('Mango', function ($resource) {
    return $resource('http://localhost:5000/mango/:mangoId');
});

Both REST-resources seem to work correctly. I have tested them with Postman by sending GET-requests to http://localhost:8080/banana/ and http://localhost:5000/mango/ . In both cases the server responsed with status code 200 OK and returned a non-empty JSON array of banana or mango object.

When I access the REST-Interfaces from within Angular controllers: Mango.query() returns correctly an array of Mangos but Banana.query() returns an empty array.

The Banana back-end is written in java with Jersey:

@Path("banana")
public class BananaCollection {
    @GET
    @Produces({MediaType.APPLICATION_JSON})
    public List<Banana> getBananas() {
        return BananaBox.getBananas(); // BananaBox handles the connection to the DB etc.
    }

I have no access to the code of the Mango back-end.

What could be the difference between those two?

JCvanDamme
  • 621
  • 5
  • 20
  • with out seeing our backend we can't say anything, your angular code looks ok,there is no issue with the code – maddygoround Sep 11 '15 at 12:31
  • 2
    What URL do you load your html page from? If the page is on `localhost:5000` then the Banana request would be a cross-domain request. – Duncan Sep 11 '15 at 12:31
  • what about excluding localhost from request $resource('/banana/:bananaId') – Dmitri Algazin Sep 11 '15 at 13:06
  • @Duncan: I load my page from localhost:8100 – JCvanDamme Sep 11 '15 at 14:05
  • 1
    In that case they are both cross-domain requests, check the Access-Control-Allow-Origin headers in both responses as it is possible the Banana server isn't setting it correctly. In that case the browser will block your code from seeing the data. – Duncan Sep 11 '15 at 14:21
  • @Duncan: Tanks, you solved the problem: Mango has Access-Control-Allow-Origin: * and Banana has no such response header. – JCvanDamme Sep 11 '15 at 14:27
  • ok, in that case I've converted the comment into an answer. – Duncan Sep 11 '15 at 14:58

1 Answers1

1

Although both addresses are on localhost the different port number means the browser counts them as different domains.

Both locations are from a different port than the original html, so the browser security will kick in and unless the response includes suitable Access-Control-Allow-Origin header the response data will be blocked from reaching the javascript.

Duncan
  • 92,073
  • 11
  • 122
  • 156