简体   繁体   English

AngularJS:发布嵌套的JSON参数

[英]AngularJS: Post nested JSON parameter

I want to post nested JSON parameters to my rails app. 我想将嵌套的JSON参数发布到我的Rails应用程序中。 It works with jquery : 它与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 ? 但是,它如何与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! 先感谢您!

Your examples are actually different, IE 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 您的示例实际上有所不同,IE在您的jquery中有一个user json对象,但您不在一个角对象中,只是提供了emailpassword ,而没有将它们包装在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 } }

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) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM