Javascript 在angularjs中读取HTTP服务器头响应消息

Javascript 在angularjs中读取HTTP服务器头响应消息,javascript,ajax,angularjs,asp.net-mvc-4,http-headers,Javascript,Ajax,Angularjs,Asp.net Mvc 4,Http Headers,我以以下方式向服务器发送Ajax请求: 应用程序设置: var appRoot = angular.module('demoApp', ['ngRoute', 'ngResource']); appRoot.config(['$routeProvider', function($routeProvider) { $routeProvider .when('/', { templateUrl: '/ngPartials/_Login.html', control

我以以下方式向服务器发送Ajax请求:

应用程序设置:

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' });
        }
]);
appRoot.factory('LoginResource', function ($resource) {
    return $resource('/Login/Login');
});
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
    });
});
工厂:

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' });
        }
]);
appRoot.factory('LoginResource', function ($resource) {
    return $resource('/Login/Login');
});
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
    });
});
控制器:

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' });
        }
]);
appRoot.factory('LoginResource', function ($resource) {
    return $resource('/Login/Login');
});
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
    });
});
现在,每当我从控制器发出Ajax请求时,我都希望读取从服务器返回的http头消息,这取决于我需要继续处理的消息

在本文中,它展示了如何读取从服务器返回的头消息。但在我的例子中,我开始搞不清楚如何阅读标题

编辑: 如果使用$resource无法实现这一点,并且只能使用$http实现,那么需要在工厂中进行哪些更改,以便不需要更改来自控制器的调用?因为我需要在整个项目中实现更改,所以更改controller中的所有REST请求对我来说是昂贵的

我试着在我的工厂里做了如下的改变,这样我就可以按照Michal的建议阅读标题了。ajax请求正确完成,我的服务器登录方法正确命中,但我通过$http请求发送的参数未绑定到我的服务器登录操作方法模型:

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;
});
服务器端模型:

public class LoginModel
{
    public string Role { get; set; }
    public string Email_Id { get; set; }
    public string Pwd { get; set; }
    public bool IsEmailValidation { get; set; }

}
行动方法:

public JsonResult Login(LoginModel oLogin)
{
   ...
}

我做错了什么?

您不能使用$resource执行此操作,它只提供高级API,您需要读取http请求。

成功回调函数有第二个参数作为回报

以下是角度文档的摘录:

使用(value,responseHeaders)参数调用成功回调。 使用(httpResponse)参数调用错误回调

链接:

因此,在回调中,您可以获得如下响应头:

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,responseHeader) {

      var headers = responseHeader();

    //do someting in callback function
    });
});

headers对象包含从服务器端返回的头文件

您可以告诉我一些方法来对服务器进行rest调用,以便我可以以类似于当前使用$response的方式使用$http,而不需要或几乎不需要更改。我在我的整个项目中使用了$response,所以请告诉我这样一种方式,这样我就可以在我的工厂中进行更改,而不需要对控制器进行太多或根本没有更改。