I am doing Ajax requests to server in following way:
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 ($resource) {
return $resource('/Login/Login');
});
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
});
});
Now whenever I do Ajax request from controller I want to read the http header messages returned from the server and depending on the message I need to proceed.
Here in this Article it has shown, how to read header messages returned from the server. But I am getting confuse how to read headers in my case.
EDIT: If there is no way to achieve this with $resource and only possible with $http what changes need to be done in factory so that I don't need to change the calling from the controller? It will be costly for me to change the all the REST request in controller since I need to implement the change in whole project.
I tried to done following changes in my factory so that I'll be able to read headers as suggested by Michal. The ajax request is being done properly and my server login method is hit properly but the parameters I am sending through angular $http request are not being binded to my server login action method model:
appRoot.factory('LoginResource', function ($resource, $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;
});
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:
public JsonResult Login(LoginModel oLogin)
{
...
}
What wrong I am doing?