0

I want to post nested JSON parameters to my rails app. It works with jquery:

$.ajax({
                type : 'POST',
                dataType : 'json',
                url : 'https://example.com/users/sign_in.json',                                
                data : {
                    user : {
                        email : $('input[name="email"]').val(),
                        password : $('input[name="password"]').val()
                    }
                }
            }).success(...

But how does it work with AngularJS? Posting non-nested parameters works:

$http({ method: 'POST', 
            url: 'https://xxx.herokuapp.com/users/sign_in.json',
            data: {email: $scope.user.email, password:$scope.user.password}
            }).success(...

But how do I post nested parameters? Any ideas? Thank you in advance!

2
  • it should work the same way. what happens when you try to post it with the user{} object? Commented Feb 20, 2014 at 22:55
  • What doesn't work about it? Your angular example isn't nested, its a simple object. Commented Feb 20, 2014 at 22:55

2 Answers 2

3

Your examples are actually different, I.E in your jquery one you have a user json object, but you don't in your angular one, you're just giving email and password and haven't actually wrapped them in a user

Have you tried changing this:

data: {email: $scope.user.email, password:$scope.user.password}

for this:

 data: { user : { email: $scope.user.email, password:$scope.user.password } }
Sign up to request clarification or add additional context in comments.

Comments

2

Instead Data use params. I hope it helps

 $http({
                method: 'POST', 
                url: 'https://xxx.herokuapp.com/users/sign_in.json',
                params: {email: $scope.user.email, password:$scope.user.password }  

  }).success(function (response, status, headers, config) {

2 Comments

My JSON should look sth like: {user:{email:"[email protected]", password: "secret"}}. But it looks like: {email: "[email protected]", password: "secret"}
params is used for 'GET' methods and data is used for 'POST' methods

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.