Skip to main content
 首页 » 编程设计

javascript之Firefox 在每次请求 Promise ajax 调用时弹出 "This web page is being redirected to a new location"- 有什么办法可以避免吗

2025年05月04日185grandyang

我有一个网络应用程序,其中包含大量 AJAXing。在我最初的测试中,这工作正常,但在我今天的测试中,Firefox(在 Mac 和 IE 上,在多台计算机上),为每个单独的用户提供弹出消息“此网页正在重定向到新位置” PUT & DELETE ajax 调用,这使得页面完全无法工作。有趣的是,弹出窗口不会出现在 GET 调用中。

PUT 和 DELETE 都使用 Angular Promise 结构,而 GET 使用标准的 $.http 调用。

这是我的 GET 代码,它不会触发弹出窗口:

 $http({ 
                method: 'GET', 
                url: $scope.faveURL 
            }).success(function (data, status, headers, config) { 
                if (data === "false") { 
                    $scope.faveempty = true; 
                    $scope.faveloading = false; 
                } else { 
                    $scope.favourites = data; 
                    $scope.faveloading = false; 
                } 
            }); 

这是我的 PUT 和 DELETE 代码,它们都会触发弹出窗口:

if (food.favourite === true) { 
                requestPromise = $http.put($scope.URL).then(function () { 
                    $scope.favourites.push(food); 
                    $scope.faveempty = false; 
                    food.loading = "none"; 
                    change = $scope.favouriteChange(food); 
                }); 
 
            } else if (food.favourite === false) { 
                requestPromise =  $http({ 
                    method: 'DELETE', 
                    url: $scope.URL 
                }).then(function () { 
                    $scope.favourites.splice($scope.favourites.indexOf(food), 1); 
                    if ($scope.favourites.length < 1) { 
                        $scope.faveempty = true; 
                    } 
                    food.loading = "none"; 
                    change = $scope.favouriteChange(food); 
                }); 
            } 

有没有其他人在使用 requestPromise 进行 Ajax 调用时遇到过这个问题?您找到任何解决方法了吗?


更新:

检查网络流量,这仅发生在以重定向响应的 AJAX 调用上。这很好,没有弹出窗口:

[15:09:58.742] GET http://redacted/api/ext/group/35/ [HTTP/1.1 200 OK 381ms] 

这会导致弹出:

[15:03:25.036] PUT http://redacted/api/ext/favorite/713 [HTTP/1.0 301 TYPO3 RealURL redirect 126ms] 

所以这是 Typo3 服务响应 PUT 和 DELETE 方法的方式的问题,而 Firefox 恰好对此有对话框警告。

请您参考如下方法:

这可能不会直接帮助您解决问题,但我遇到了同样的问题,这是因为我的请求类型有误。我相信它可以帮助其他人。

请记住,我使用的是 jQuery。

原代码:

      $.ajax({ 
        url: link, 
        type: 'json', 
        success: function(html) { 
          return _this.ajaxLoadIn(html); 
        }, 
        error: function(e) { 
          return alert('Sorry, something went wrong, please try again'); 
        } 
      }); 

解析代码:

      $.ajax({ 
        url: link, 
        dataType: 'json', 
        type: 'get', 
        success: function(html) { 
          return _this.ajaxLoadIn(html); 
        }, 
        error: function(e) { 
          return alert('Sorry, something went wrong, please try again'); 
        } 
      }); 

如您所见,我设置了错误的“类型”参数。它应该是 GET 或 POST。希望这可能会有所启发。