Check that AngularJS $resource Request Server Side in .NET MVC

前端 未结 1 376
名媛妹妹
名媛妹妹 2020-12-17 16:53

is there a way to tell if a request is an Angular (1.1.5) $resource request. I\'m pretty much looking for a \"Request.IsAjaxRequest()\" method for this type of request.

相关标签:
1条回答
  • 2020-12-17 17:22

    I don't know well MVC3 but you can set a custom header for all request from AngularJS.

    Then on server side you just have to get this header and do what you want with request from angular.

    To have custom header in AngularJS just do this :

    angular.module('myModule', [])
    
        .config(['$httpProvider', function($httpProvider) {
    
            $httpProvider.defaults.headers.common["FROM-ANGULAR"] = "true";
    
        }])
    

    For use the X-Requested-With you have to do this too :

    $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
    

    It's not set by default anymore because a lot part of the community have to delete this header to enable CORS request

    0 讨论(0)
提交回复
热议问题