0

I am a newbie with Ruby on Rails and I am trying to figure out ways to connect Angular to RoR in a very simple way

Here is my service

mWebApp.service('mWebSrvc', function($http, $log) {
this.getCustomers = function() {
    $http({
        method : 'GET',
        url : 'http://127.0.0.1:3000/api/customers/'
    }).success(function(data, status, headers, config) {
        $log.log('Done');
        angular.forEach(data, function(c) {
            $log.log(c.Title);
        });
        customers = data;
        return customers;
    });     
};
});

When I look under the Net tab in Firebug, I see OPTIONS /api/customers/ 404 Not Found, but if I click on the Response tab within, then I see the JSON file - WTF? And not the JSON tab - again, WTF?

Under Firebug's console -

"NetworkError: 404 Not Found  - http://numberForLocalHost:3000/api/customers/"

My Rails server is running in daemon mode - numberForLocalHost:3000 - is this what the issue might be? That it should be calling a true api

If I paste the URL above into any web browser, then I can see the JSON

As usual, thanks in advance

kronus
  • 826
  • 1
  • 11
  • 26

2 Answers2

0

You're getting an OPTIONS request because your browser believes this is a cross origin request.

See this question for example. Is your RoR app also serving your client side angular? If not, you should decide whether it can be (there shouldn't be a reason not to), or you need to reply to the pre-flight OPTIONS request from your server that you are seeing.

Community
  • 1
  • 1
Ed Hinchliffe
  • 17,456
  • 8
  • 42
  • 67
  • When I try to bring up my index.html file (Angular) through the localhost:3000, then it automatically goes to the RoR index.html.erb file, which has no angular. My index.html is in the root of the directory and calls files from the /angular/scripts/view/ directory - it is running on localhost:8888 Should I place the contents of the index.html file into the index.html.erb file? – kronus Mar 27 '14 at 01:06
0

I had the very same issue with my Rails + Angular app. I had my cors well set up in my rails app but still nothing, I still got a 404 not found in the angular app. This could be the reason: Perhaps you have "angular-in-memory-web-api": '0.x.x' in your package.json and also imported in your app.module.ts, as InMemoryWebApiModule and InMemoryDataService. These apparently intercept all calls to an API preventing them from ever reaching your back-end server. When I Removed those dependencies and their declarations, all of a sudden my app started working normally!

Look at this answer for more information.

Denn
  • 665
  • 7
  • 17
  • Thanks @Denn for replying. I posted this question over five years ago and have since moved away from RoR. Sorry that I cannot remember the solution, but thank you for the info – kronus Aug 13 '19 at 15:28