服务

comaxApp.factory('user', function($http) {
  return {
    login: function(loginInfo) {
      $http({
        url: './data/LogIn',
        method: "POST",
        data: loginInfo,
        headers: {
          'Content-Type': 'application/json'
        }
      }).success(function(data, status, headers, config) {
        $scope.users = data.users;
      }).error(function(data, status, headers, config) {
        $scope.status = status + ' ' + headers;
      });
    }
  };
});


控制者

$scope.login = function() {
    var loginInfo = {
        "user": "admin",
        "password": "123"
    };
    loginInfo = JSON.stringify(loginInfo);
    user.login(loginInfo).then(function(users) {
        $scope.users = users.data;
    }, function(status) {});
};


MVC控制器

public string LogIn(string loginInfo) {

  var obj = new JavaScriptSerializer();
  var result = obj.DeserializeObject(loginInfo);


  var db = new comaxDataEntities();
  var linq = db.accounts.Where(u => u.userName == loginInfo);
  var useraccount = linq.FirstOrDefault < account > ();

  var javaScriptSerializer = new JavaScriptSerializer();
  string jsonString = javaScriptSerializer.Serialize(useraccount);
  return jsonString;

}


嗨,同志,我的问题是当我将数据传递给C#控制器时,我的loginInfo为null负载正常,并显示stringify loginfo,但我的方法未获取logInfo。请帮助我,我是C#的新手

最佳答案

您的服务应将字符串化数据传递给服务器,因为您在操作时将参数声明为字符串,例如JSON.stringify({loginInfo: loginInfo})



$http({
    url: './data/LogIn',
    method: "POST",
    data: JSON.stringify({
        loginInfo: loginInfo
    }),
    headers: {
        'Content-Type': 'application/json'
    }
}).success(function(data, status, headers, config) {
    $scope.users = data.users;
}).error(function(data, status, headers, config) {
    $scope.status = status + ' ' + headers;
});


对于更好的方法,我希望在服务器端创建loginInfo类,该类将直接映射到类,您无需反序列化它。

09-17 07:33