REST post value from AngularJS always NULL

1,833 views
Skip to first unread message

jusufd

unread,
Jun 2, 2013, 11:34:29 AM6/2/13
to ra...@googlegroups.com
Hi All,

I am always having a NULL values when I post the value using Angular JS service to the REST api. When I tried using http post from Railo, I can get the value right. Do you guys know what's I should set from Angular. THANKS.

Here is the Angular controller:
var store1App = angular.module('store1App', [ 'store1AppService']).config(
    function($routeProvider,$locationProvider,$httpProvider)
    {
        $routeProvider.when('/');
        $httpProvider.defaults.headers.post  = {'Content-Type': 'application/x-www-form-urlencoded'};
    }
);
function Login($scope,Login) {
    $scope.error_message = false;
    $scope.LoginButton = function () {
        var LoginResult = Login.Normal(
            {
                email:$scope.email,
                password:$scope.password,
                recaptcha_challenge_field: Recaptcha.get_challenge(),
                recaptcha_response_field: Recaptcha.get_response()
            },
            function(result) {
                if (typeof result['success'] === "undefined")
                {
                    if (result['showCaptcha']) {
                         Recaptcha.create(
                        $scope.recaptcha_public_key,
                        "captchaSection",
                        {
                             theme: "red",
                             callback: Recaptcha.focus_response_field
                        });
                        $("#captchaSection").css("margin-bottom","18px");
                     }
                    $scope.error_message = true;
                    $scope.errors = result['error'];
                  }
                  else
                  {
                      $scope.error_message = false;
                    if (typeof result['url2redirect'] === 'undefined') {
                        location.reload();
                    } else {
                        window.location = result['url2redirect'];
                    }    
                  }    
            }
        );
    };
};

Here is the Angular service:
angular.module('store1AppService',['ngResource' ])
.factory('Login',
    function($resource) {
        return $resource('rest/jd/user/login.json',
            {},
            {
                Normal:
                {
                    method: 'POST',
                    email: '@email',
                    password: '@password',
                    recaptcha_challenge_field: '@recaptcha_challenge_field',
                    recaptcha_response_field: '@recaptcha_response_field'
                }
            }               
        );
})

jusufd

unread,
Jun 2, 2013, 7:13:02 PM6/2/13
to ra...@googlegroups.com
Here the REST component. If I post to the page I can get the value. So it might be have a problem with my REST setting in RAILO. But I don't know where is it. Help me please. THANKS.


component restpath="user" rest="true" 
{
remote any function login(
string email restargsource="Form",
string password restargsource="Form",
string recaptcha_challenge_field restargsource="Form",
string recaptcha_response_field restargsource="Form"
) restpath="/login/" httpmethod="POST" 
{
var local = structnew();
local.retval = {
'email' = arguments.email,
'password' = arguments.password,
'recaptcha_challenge_field' = arguments.recaptcha_challenge_field,
'recaptcha_response_field' = arguments.recaptcha_response_field
};
return local.retval;
}
}

AJ Mercer

unread,
Jun 2, 2013, 8:25:04 PM6/2/13
to ra...@googlegroups.com
Are you able to see the status code ?

Try setting request header for
Content-type 
Accepts

Have you tried calling the ReST service in something like Poster (chrome/Firefox extension)? 
--
Did you find this reply useful? Help the Railo community and add it to the Railo Server wiki at https://github.com/getrailo/railo/wiki
 
 
If you don't have time, add a request to the Railo Server wiki to-do page at https://github.com/getrailo/railo/wiki/Todo
---
You received this message because you are subscribed to the Google Groups "Railo" group.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


--

AJ Mercer
<webonix:net strength="Industrial" /> | <webonix:org community="Open" />
http://twitter.com/webonix

jusufd

unread,
Jun 4, 2013, 12:10:54 AM6/4/13
to ra...@googlegroups.com
Hi AJ,

I think I found the problem. AngularJS always post the JSON data and RAILO didn't automatic deserialize the JSON so the parameter always NULL.  I have a question how I can setup RAILO to automatic deserialize the JSON request. Or how I can set in AngularJS not to serialize the request into JSON.

Thanks.

jusufd

unread,
Jun 4, 2013, 12:20:39 AM6/4/13
to ra...@googlegroups.com
Hi AJ,

I just found the solution which is by transforming the AngularJS request like this below

$httpProvider.defaults.headers.post  = {'Content-Type': 'application/x-www-form-urlencoded'};
$httpProvider.defaults.transformRequest = function(data) {
        return data != undefined ? $.param(data) : null;
}

Thanks.



On Sunday, June 2, 2013 5:25:04 PM UTC-7, AJ Mercer wrote:

AJ Mercer

unread,
Jun 4, 2013, 12:25:50 AM6/4/13
to ra...@googlegroups.com
by default, Railo uses 'application/json'
and will do the JSON serialisation.

In Railo, you set this with the 'produces' and 'consumes' attributes of the function.

I am pretty sure it is case sensitive, but you can have a list
eg
produces="application/json,APPLICATION/json,APPLICATION/JSON,text/json" 
Reply all
Reply to author
Forward
0 new messages