2

Why it's pass null in model variable? Where is mistake?

enter image description here

Controller:

    [HttpGet]
    [AllowAnonymous]
    public JsonResult LoginAngular(LoginViewModel model, string returnUrl)
    {
        Console.WriteLine(model.UserName);
        return Json(model.UserName,JsonRequestBehavior.AllowGet);
    }

Angular:

var login = angular.module('LoginApp', []);
login.controller('LoginCtrl', function ($scope, $http) {
    $scope.name = 'Login';
    $scope.model = { UserName: 'nothing', Password: 'nothing' };
    $scope.model.UserName = "Test";
    $scope.model.Password = 'Test';
    $scope.returnUrl = '/Account/TestLogin';

    $scope.onClick = function () {
        console.log($scope.model);
        $http({
            method: 'GET',
            url: '/Account/LoginAngular',
            params: { model: $scope.model, returnUrl: $scope.returnUrl }
        }).success(function (response) {
            console.log(response);
        });
    };
});
2
  • from my understading "get" is for getting data, "post" is for pushing data so you should change that. you are also trying to map an angular model to an mvc model. make sure that the terms defined in your mvc view model match exactly with what you defined in angular Commented May 15, 2016 at 13:30
  • get request don't have request body, you need and should use POST for operation such as login or registration. Commented May 15, 2016 at 14:08

2 Answers 2

1

Try the following solution:

  $http({
        url: '/Account/LoginAngular',
        method: "POST",
        data: {model: $scope.model, returnUrl: $scope.returnUrl},
        headers: {  
            "Content-Type": "application/json"  
        }  
    }).then(function(response) {
        // success
    }, 
    function(response) { // optional
        // failed
    });

Alternatively you can try:

   $http.post('/Account/LoginAngular',{model: $scope.model, returnUrl: $scope.returnUrl}) 
       .success(function (data, status, headers, config) {
            //get response
        })
        .error(function (data, status, header, config) {
            //get error details
        });
Sign up to request clarification or add additional context in comments.

2 Comments

using $.param() assumes jQuery is being used and it shouldn't be used for "application/json" . angular also has a built in serializer that negates needing $.param() if you want to use form encoding. See docs.angularjs.org/api/ng/service/$httpParamSerializerJQLike
Noted. Thank you for the info.
0

Use POST method to send the data.

var login = angular.module('LoginApp', []);
login.controller('LoginCtrl', function ($scope, $http) {
$scope.name = 'Login';
$scope.model = { UserName: 'nothing', Password: 'nothing' };
$scope.model.UserName = "Test";
$scope.model.Password = 'Test';
$scope.returnUrl = '/Account/TestLogin';

$scope.onClick = function () {
    console.log($scope.model);
    $http({
        method: 'POST',
        url: '/Account/LoginAngular?returnUrl=$scope.returnUrl',
        data: $scope.model,
    }).success(function (response) {
        console.log(response);
    });
  };
});

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.