I´m developing a web application where I have a front-end built with AngularJS that consumes Rest Services provided by a back-end API Rails application.
I would like to use the Angular $resource to get the Json objects in the backend API.
Client and Server applications run in different servers so far. So, angular client is in server-path:1234/app and backend api application runs in another-server-path:3000/.
Where server-path and another-server-path are localhost.
Both sides are developed, and works independently. I mean:
- When I run another-server-path:3000/boats in a browser I get the list of boats, so the Restful service looks to be working.
- I did a simulation in the client side, so, in my service I replaced the real url with a simulated url to a json data source. This code works too.
Now, I want to call the backend restful service in another-server-path:3000/boats from the angular service, but it´s now showing any data, neither I can see any get request in servers logs.
This is my angular service code:
angular.module('myApp.services', ['ngResource']).
// value('version', '0.1');
factory('Boat', function($resource){
return $resource('another-server-path:3000/:boatId', {}, {
query: {method:'GET', params:{boatId:'boats'}, isArray:true}
});
});
and this is my backend rails controller:
def index
@boats = Boat.all
respond_to do |format|
format.html
format.xml { render :xml => @boats}
format.json { render :json => @boats}
end
end
What am I missing? Please help.
UPDATE: In Angular app, I changed my service file with:
return $resource('another-server-path\\:3000/:boatId', {}, {
query: {method:'GET', params:{boatId:'boats'}, isArray:true}
I also added this line to my app.js file:
delete $httpProvider.defaults.headers.common["X-Requested-With"];
and now I got the request in the api server. The controller is also executed. I got the boats from the DB. However I´m not able to send them back in JSON to Angular.
This is my api Controller:
def index
@boats = Boat.all
puts @boats
render :json => @boats
end
and this is the log I got when it´s called:
Started GET "/boats" for 127.0.0.1 at 2013-09-26 17:05:49 +0200
Processing by BoatsController#index as HTML
Boat Load (2.0ms) SELECT `boats`.* FROM `boats`
#<Boat:0x0000000675d9f0>
#<Boat:0x000000089e74a8>
#<Boat:0x000000089e71b0>
#<Boat:0x000000089e6e40>
Completed 200 OK in 66ms (Views: 3.0ms | ActiveRecord: 7.0ms)
As you can see, first, it says it is been processed as HTML instead of JSON. An second it is not sent to Angular back.
$resource('another-server-path\\:3000/:boatId'