angularjs $ http.post,参数为null

时间:2015-11-06 06:58:30

标签: angularjs model-view-controller

我在登录页面使用angular但是将null参数返回给控制器。

查看页面:这是我在视图页面中的代码

  <div ng-controller="LoginController">
   <form novalidate name="f1" ng-submit="Login()">

        <div style="color:rgb(142,2,2)">{{Message}}</div>
        <table ng-show="!IsLogedIn">
            <!-- Here  ng-show="!IsLogedIn" means I want to hide table after loged in-->
            <tr>
                <td>Username : </td>
                <td>

                    <!-- Here ng-class="Submitted?'ng-dirty':''" Means if submitted (clicked submit button) then make this dirty state
                        for show red border-->
                    <input type="text" ng-model="Login.Username" name="cUsername" ng-class="Submitted?'ng-dirty':''" required autofocus />
                    <span class="error" ng-show="(f1.cUsername.$dirty || Submitted) && f1.cUsername.$error.required">Username required</span>

                    <!-- ng-show="(f1.cUsername.$dirty || Submitted) && f1.cUsername.$error.required" Means I want to show this span only when
                        the control cUsername is invalid-->
                </td>
            </tr>
            <tr>
                <td>Password : </td>
                <td>
                    <input type="password" ng-model="Login.pass" name="cPassword" ng-class="Submitted?'ng-dirty':''" required autofocus />
                    <span class="error" ng-show="(f1.cPassword.$dirty || Submitted) && f1.cPassword.$error.required">Password required</span>
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="submit" value="Login" />
                </td>
            </tr>
        </table>
    </form>
</div>

我需要在其他工厂中使用此工厂的返回值。 但得到空参数 mycontroll.js

 angular.module('MyApp') 
.controller('LoginController', function ($scope, LoginService) {
    $scope.IsLogedIn = false;
    $scope.Message = '';
    $scope.Submitted = false;
    $scope.IsFormValid = false;

    $scope.LoginData = {
        Username: '',
        Password: ''
    };

    //Check is Form Valid or Not // Here f1 is our form Name
    $scope.$watch('f1.$valid', function (newVal) {
        $scope.IsFormValid = newVal;
    });

    $scope.Login = function () {
        $scope.Submitted = true;
        if ($scope.IsFormValid) {
            LoginService.GetUser($scope.LoginData).then(function (d) {
                if (d.data.Username != null) {
                    $scope.IsLogedIn = true;
                    $scope.Message = "Successfully login done. Welcome " + d.data.Name;

                }
                else {
                    alert('Invalid Credential!');
                }
            });
        }
    };

})
.factory('LoginService', function ($http) {
    var fac = {};
    fac.GetUser = function (d) {
        return $http({
            url: '/login/Login',
            method: 'POST',
            data: JSON.stringify(d),
            headers: { 'content-type': 'application/json' }
        });
    };
    return fac;
});

logincontrol

[HttpPost]
        public virtual JsonResult Login(Login d)
        {
            using (CrudAngularEntities dc = new CrudAngularEntities())
            {
                var user = dc.Users.Where(a => a.Username.Equals(d.Username) && a.pass.Equals(d.pass)).FirstOrDefault();
                return new JsonResult { Data = user, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            }
        }
    }

enter image description here

1 个答案:

答案 0 :(得分:0)

您正在将Traceback (most recent call last): File "C:\Users\u_tem0m\Dropbox\Wrk\sgo\broken.py", line 17, in <module> for t in Test.select(Test.name, fn.group_concat(Test.score)).order_by(Test.name): File "C:\Program Files\Python 3.5\lib\site-packages\peewee.py", line 1938, in next obj = self.qrw.iterate() File "C:\Program Files\Python 3.5\lib\site-packages\peewee.py", line 1995, in iterate return self.process_row(row) File "C:\Program Files\Python 3.5\lib\site-packages\peewee.py", line 2070, in process_row setattr(instance, column, func(row[i])) File "C:\Program Files\Python 3.5\lib\site-packages\peewee.py", line 874, in python_value return value if value is None else self.coerce(value) ValueError: invalid literal for int() with base 10: '1,2' 传递给函数$scope.LoginData。相应的服务器模型为LoginService.GetUser。因此它的签名需要相同。

Login

然后你将ng模型映射到其他东西(到$scope.LoginData = { pass : '', Username: '' }; obj)。您可能需要用

替换用户名和密码的输入
Login