【问题标题】:AngularJS $Http not triggered on Edge/IEAngularJS $Http 未在 Edge/IE 上触发
【发布时间】:2016-12-02 09:48:49
【问题描述】:

我有以下代码,在 Mozilla、Chrome 上运行良好,但在 Microsoft Edge 或 Internet Explorer 上运行良好。这就是发生的事情。

当用户加载网页时,我会触发以下代码:

    $http.get("/api/items")
        .then(function (response) {
            angular.copy(response.data, $scope.items);
        }, function (error) {
            $scope.errorMessage = error.statusText;
        })
    .finally(function () {
        $scope.isBusy = false;
    });

将所有值拉入<ul>。这部分在所有浏览器上都能正常工作。但是,当我触发以下一个时:

    $scope.interval = $interval(function () {
        $http.get("/api/items")
            .then(function (response) {
                //success

                //loop through the response
                angular.forEach(response.data, function (value, key) { ... more code here}

然后突然响应不再来自我的 web api。我的意思是,当我调试我的应用程序并在我的 GET api 中放置断点(我的意思是在我的 MVC 控制器中的 Visual Studio 下)时,我看不到 Microsoft Edge 触发了 get 调用。

这是 Chrome 中网络检查器的外观:

这个来自 Edge:

据我了解,Edge 只对我的 GET 进行了一次真正的 XHR 调用,而所有其余的都只是从内存中缓存?

如何解决这个问题?为什么会发生这种情况?

【问题讨论】:

    标签: angularjs debugging xmlhttprequest asp.net-core-mvc microsoft-edge


    【解决方案1】:

    最后我发现问题出在 Angular 的存储缓存上。有两个选项可以使用(取自其他 StackOverflow 帖子):

    1) Angular 方法(由于未知原因对我不起作用):

    myModule.config(['$httpProvider', function($httpProvider) {
        //initialize get if not there
        if (!$httpProvider.defaults.headers.get) {
            $httpProvider.defaults.headers.get = {};    
        }    
    
        // Answer edited to include suggestions from comments
        // because previous version of code introduced browser-related errors
    
        //disable IE ajax request caching
        $httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
        // extra
        $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
        $httpProvider.defaults.headers.get['Pragma'] = 'no-cache';
    }]);
    

    2) ASP.NET对GET方法的修饰方式:

    [ResponseCache(NoStore = true, Duration = 0)]
    

    【讨论】:

      猜你喜欢
      • 2017-10-16
      • 1970-01-01
      • 1970-01-01
      • 2017-01-09
      • 1970-01-01
      • 2020-12-08
      • 2014-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多