0

I have an Angular.js app that sends a POST request to a Rails server endpoint and I'm having trouble passing and reading the parameters.

This is how I send the data

$scope.submitForm = function(info) {

    $http({
      method: 'POST',
      url: app.apiServerUrl + '/complaints/create',
      data: info,
      headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).success(function(data, status, headers, config) {

      // this callback will be called asynchronously
      // when the response is available
    }).error(function(data, status, headers, config) {
     // called asynchronously if an error occurs
     // or server returns response with an error status.

  });
};

The info object passed is a object in the form: { title: "something", ocurrence_place: "something" }.

And in my controller create action I have this:

def create
  p params
  p params[:title]
end

but this is what gets printed in the logs

{"{\"title\":\"foo\",\"ocurrence_place\":\"bar\"}"=>nil, "controller"=>"complaints", "action"=>"create"}
nil

So, as you can see, I can't access params hash attributes. How can I fix this so the controller in my controllers works and prints what is expected?

1 Answer 1

1

It looks to me like your info object isn't really an object but a string of JSON.

Try changing data: info to data: JSON.parse(info) or correcting whatever is calling submitForm(info) so that the info argument is really a object.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, I ended up actually using JSON.stringify, but you gave me the first idea :D
I believe the fix is to change data: info, into data: JSON.stringify(info),

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.