I am doing Ajax requests to server in following way:
我正在以下列方式向服务器发送Ajax请求:
App Setup:
应用程序设置:
var appRoot = angular.module('demoApp', ['ngRoute', 'ngResource']);
appRoot.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/', { templateUrl: '/ngPartials/_Login.html', controller: 'LoginCtrl' })
.otherwise({ redirectTo: '/ngPartials/_Login.html', controller: 'LoginCtrl' });
}
]);
Factory:
工厂:
appRoot.factory('LoginResource', function ($http) {
var loginResource = {
response:{},
get: function (param) {
$http.get('/Login/Login', param).
success(function (data, status, headers, config) {
this.response = data;
}).
error(function (data, status, headers, config) {
alert('error');
});
}
}
return loginResource;
});
Controller:
控制器:
appRoot.controller('LoginCtrl', function ($scope, LoginResource) {
//Make a get request on login resource
$scope.User = LoginResource.get(
{
Role: val,
Email_Id: $scope.Email,
Pwd: $scope.Password
}, function (response) {
//do someting in callback function
});
});
But I don't know what wrong I am doing in above code, the parameters (Role,Email_Id,Pwd) are not being binded to my server Action method when I do get request to it. The method is hit properly but the model values are not being binded.
但是我不知道我在上面的代码中做错了什么,当我收到请求时,参数(角色、Email_Id、Pwd)没有绑定到我的服务器操作方法。方法被正确命中,但是模型值没有绑定。
server side model:
服务器端模型:
public class LoginModel
{
public string Role { get; set; }
public string Email_Id { get; set; }
public string Pwd { get; set; }
public bool IsEmailValidation { get; set; }
}
Action method:
操作方法:
[HttpGet, ActionName("Login")]
public JsonResult Login(LoginModel oLogin)
{
...
}
When I call above action method from angular, the method is being hit, but all the properties in the object oLogin are null. What going wrong with my above code?
当我从角度调用上述操作方法时,该方法正在被命中,但对象oLogin中的所有属性都为空。我上面的代码出了什么问题?
EDIT:
编辑:
Previously instead of $http, I was using $resource as follows and everything was working exactly fine.
之前,我使用的不是$http,而是使用$resource,一切都很正常。
appRoot.factory('LoginResource', function ($resource) {
return $resource('/Login/Login');
});
1 个解决方案
#1
1
In LoginResource, the second parameter to $http.get is the config, not the params...so change it to the following:
在LoginResource中,$http的第二个参数。get是配置,而不是params…所以把它改成以下几点:
get: function (param) {
$http.get('/Login/Login', { params: param }).
#1
1
In LoginResource, the second parameter to $http.get is the config, not the params...so change it to the following:
在LoginResource中,$http的第二个参数。get是配置,而不是params…所以把它改成以下几点:
get: function (param) {
$http.get('/Login/Login', { params: param }).