AngularJS 中的 HTTPS 'GET'

HTTPS 'GET' in AngularJS

我正在使用 Cordova 和 AngularJS 开发移动应用程序。我正在努力 运行 使用 AngularJS $http 服务对 Amazon S3 服务器进行 HTTPS 'Get' 调用。调用失败:

SSLHandshake: Remote host closed connection during handshake

这是我调用 S3 服务器的方式:

$http({method: 'GET' , url: path});

path 变量是到 S3 的 HTTPS url。如果 URL 不是 HTTPS,这就可以正常工作。 我在使用 Cordova FileTransfer class 时也遇到了这个问题,但是我能够通过在开始下载时将 trustAllHosts 设置为 true 来解决它。 AngularJS $http 服务无法做到这一点。

有人知道我怎么解决这个问题吗?任何帮助将不胜感激。

在 AngularJS 中,您应该使用 $sce.trustAsResourceUrl(url); 来使用 HTTPS 等安全 URL。

示例代码:

$scope.trustSrc = function (path) {
    if (path) {
        if (path.indexOf('https') === -1) {
            path = path.replace('http', 'https');
        }
    }
    return $sce.trustAsResourceUrl(path);
};

这应该是 return 一个可以在您的 GET 请求中使用的安全路径,如下所示:

$scope.trustedPath = $scope.trustSrc(path);
$http({method: 'GET' , url: trustedPath});
    getUser().then(function (response) {
        $scope.users = response.data;
    }).catch(function (err) {
        debugger;
    })

    function getUser() {
        return $http.get("https://jsonplaceholder.typicode.com/users")
    }