0

I have a factory that exposes a create() function which uses $http.post() underneath. For some reason when it is called from an ng-click the request is being formatted as HTML. All of the params are where I want them to be, but Rails is processing the request as HTML.

The factory:

orizabaServices.factory('requestTypeFactory', ["$http", function($http){
    var urlBase = '/request_types';
    var requestTypeFactory = {};

    requestTypeFactory.create = function(params){
      data = {};
      data.authenticity_token =  $("meta[name='csrf-token']").attr("content");
      data.request_type = params;
      return $http.post(urlBase, data)
    };

    return requestTypeFactory;
  }
])

The Error:

Started POST "/request_types" for 127.0.0.1 at 2014-08-01 17:10:00 +0000
Processing by RequestTypesController#create as HTML

3 Answers 3

2

You can specify the format of the request adding '.json' to your urlBase. For example:

var urlBase = '/request_types.json';
Sign up to request clarification or add additional context in comments.

1 Comment

I understood that the post() was supposed to set a default JSON format, so I see this as kind of a workaround. I can't blame rails because it handles the request fine when it is formatted correctly. But this is a simple and reasonable solution.
0

You need to add "full URL" for urlBase. If you request POST method on your local machine

var urlBase = 'http://127.0.0.1/request_types'; 

try this

1 Comment

Passing the full URL doesn't affect. Thanks though!
0

It's because by default rails is considerng all requests as HTML. You can change this behaviour in your config/routes.rb file like this:

resources :request_types, :defaults => {:format => 'json'}

Comments

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.